Creating a new SugarBean with a pre-created UUID

I have a custom API endpoint that we use to send a large number of records at a time. The endpoint works for updating records and inserting new records without a pre-created UUID, but when we send records with a pre-created UUID, we get the UUID back as a response, but the record is not written into the database.

To answer the questions this is going to cause, we do not use the bulk API endpoint due to a performance issue, since it is a http request to make multiple http requests. Also, we are a SugarCloud client so there is a 20 requests a minute limit, which is easy to violate. 

Does anyone have any insight as to why SugarBean might have an issue with new records with pre-created UUIDs?

Here is the code for the custom endpoint.

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public function setEntriesMethod($api, $args)
{
if (empty($args)) {
return false;
}
$results = array();
foreach ($args as $module => $records) {
if (is_array($records)) {
foreach ($records as $fieldsArray) {
$sugarBean = $this->_processEntry($module, $fieldsArray);
$results[$module][] = $sugarBean->id;
}
}
}
return $results;
}
private function _processEntry($module, $fieldsArray)
{
$Logger = Factory::getLogger('default');
if (array_key_exists('id', $fieldsArray)) {
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

  • Could you update your code to use the REST API instead of the 10+ year deprecated SOAP API?

    There is built-in functionality in Sugar 11/12. There is an additional field sync_key and matching REST API functions which allow for the use of an external ID.

    If you need to continue using the SOAP API. The SugarBean has a parameter new_with_id when your record is created on line 25 assign the ID and set new_with_id = true;

    /**
    * When createing a bean, you can specify a value in the id column as
    * long as that value is unique. During save, if the system finds an
    * id, it assumes it is an update. Setting new_with_id to true will
    * make sure the system performs an insert instead of an update.
    *
    * @var BOOL -- default false
    */
    var $new_with_id = false;
  • If you are saving a Sugar Bean with a specific id that you have pre-created then you need to set the "new_with_id" attribute to true to indicate that you are providing your own id value. Otherwise, I believe, Sugar will simply save the new record with a generated id as if you had supplied nothing in this field.

    You should be able to do this easily enough in your code above by just doing:

    $sugarBean->new_with_id = true;

    right after your $sugarBean = BeanFactory::newBean($module); call.

    I suspect your records might be saving OK but not with the id you are supplying. You can find details of the processing for a Sugar Bean here:

    https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_12.0/Data_Framework/Models/SugarBean/#Saving_a_Bean_with_a_Specific_ID

    Hopefully this is the cause of your issue and you can find a fix here.

    Thanks,

    JH.

    EDIT: Gaah!! Beaten by mere seconds to an answer by Jeff Grimacing

  • First off thanks, I have added the new_with_id flag and currently repairing and rebuilding for testing. Yes this is old code that I found online and made a few modifications to suit our needs. I thought it was REST API instead of SOAP API, if you arew willing to point out what I need to change I will to update it so it will continue to work for us in the future. Also, I didn't put the entire endpoint code just the 2 functions that make it work, I left out the registerApiRest function and the class defintion.

  • Thank you I am currently testing this solution right now and will update once I have finished my testing.

  • Yes, this solved my problem.

  • Yes, this solved my problem.

  • Oops, sorry all. I tried to click on the Reply button but hit the down vote by mistake. I then had to switch it to an up vote as I couldn't unvote it. Apologies for voting for my own solution Disappointed

  • LOL it happens but looks like it is fixed now.