How can I set the "Created By" field when importing data through the API?

I'm importing some data through the API, and I found I was not able to set the 'created_by' field when creating records.  I thought I used to be able to do this, but maybe I am mistaken.

POST: <sugar_url>/rest/v11_16/Accounts

{
    "name" : "test5",
    "date_entered":"2024-10-17T12:34:12-04:00",
    "created_by":"seed_will_id"
}

I did notice that in SugarBean, there is a flag you can set (set_created_by), and so I tried that as well, but with the same result

{
    "name" : "test5",
    "date_entered":"2024-10-17T12:34:12-04:00",
    "created_by":"seed_will_id",
    "set_created_by":false
}

I see there is documentation to allow this to be done through the import wizard, but I haven't tested to see if it works. I've too many records to be importing data that way.

https://support.sugarcrm.com/documentation/sugar_versions/14.0/serve/application_guide/import/#Updating_the_Date_Created_and_Created_By_ID_Fields

In the file:

SugarBeanAPiHelper.php:

There is a section that loops through all of the fielddefs and then if there are any matches in the submitted data, it'll add it to the save event.  however, it looks like 'set_created_by' isn't in the field defs, so it never gets passed to the SugarBean save() process.

I can get it to work if I modify the core file like this so sugar thinks that set_created_by is a field.

Is this a bug, or intentional?

Thanks!

Parents
  • As far as I know from old projects the creator can only be set with an additional PUT call having set_created_by set to true.

    The idea behind is that the POST call always sets the created_by to the user running the REST code ( who really created this record ) and if you want to change this user to another id you must run an update on the created_by field with set_created_by explicitely set to true.

    If I‘m wrong correct my guys but don‘t change the code because you need this feature sometimes.

  • I did find why updating the records to set the created_by doesn't work anymore.

    Starting in version 11, there was a change made to clients/base/api/ModuleApi.php

        protected $disabledUpdateFields = array(
            'deleted',
        );

    changed to:

        protected $disabledUpdateFields = array(
            'deleted',
            'created_by',
        );

    So the 'created_by' field gets removed by "ModuleApi".

  • Ok, sometimes I do not see the forest because there are too many trees.

    You can write a simple before_save logic hook which sets the created_by field to the creation user value like this:

    <?php
    if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
    
    class setAccountData {
    
    	function setCreator(&$bean, $event, $arguments){
    		
    		if(($event=="before_save")){
    			if (($bean->created_by != $bean->user_id_c) &&
    			    ($bean->user_id_c != ""))
    			{
    				$bean->created_by = $bean->user_id_c;
    			}
    		}		
    	}
    
    } //end class setAccountData
    

Reply
  • Ok, sometimes I do not see the forest because there are too many trees.

    You can write a simple before_save logic hook which sets the created_by field to the creation user value like this:

    <?php
    if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
    
    class setAccountData {
    
    	function setCreator(&$bean, $event, $arguments){
    		
    		if(($event=="before_save")){
    			if (($bean->created_by != $bean->user_id_c) &&
    			    ($bean->user_id_c != ""))
    			{
    				$bean->created_by = $bean->user_id_c;
    			}
    		}		
    	}
    
    } //end class setAccountData
    

Children
No Data