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
  • <?php
    if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
    class CustomCreatedByAPI extends SugarApi
    {
        public function registerApiRest()
        {
            return array(
                'CustomCreatedBy' => array(
                    'reqType' => 'POST',
                    'noLoginRequired' => false,
                    'path' => array('CustomCreatedBy'),
                    'pathVars' => array(''),
                    'method' => 'CustomCreatedByMethod',
                    'shortHelp' => 'Forces update to created by'
                ),
            );
        }
        public function CustomCreatedByMethod($api, $args)
        {
            $record = BeanFactory::retrieveBean($args['module'],$args['id']);
            if(!isset($record->id)){
                //not a valid record. abort.
                return "not a record";
            }
            $user = BeanFactory::retrieveBean("Users",$args['created_by']);
            if(!isset($user->id)){
                //not a valid user.  abort.
                return "not a user";
            }
            $result = $record->db->updateParams(
                $record->getTableName(),
                $record->getFieldDefinitions(),
                ['created_by' => $user->id],
                ['id' => $record->id]
            );
            return json_encode($result);
        }
    }

    POST /rest/<version/CustomCreatedBy

    {
        "module":"<module_name>",
        "id":"<record_id>",
        "created_by": "<user_id>"
    }

  • Perhaps a little addition:
    If you add a date you can migrate the full creation information from an external system to Sugar.

Reply Children
No Data