AnsweredAssumed Answered

Cannot add related Account and Contact to a Case record on a custom API endpoint

Question asked by Artis Plocins on Jun 26, 2017
Latest reply on Jul 6, 2017 by Artis Plocins

I have a working custom REST API endpoint in Sugar 7.8 and I even created Note records to test that it works.

 

This endpoint does not require authentication but sets the global $current_user to the system user in order to allow the use of BeanFactory.

 

I have managed to find a Contact by an email address and then find the Account that is related to that contact, but I now need to add both the Account and the Contact that I retrieved to a new Case and save it - all when the API endpoint is called.

 

I have been digging through SugarCRM source code, community and Google to try and find a solution for this, I have even tried extending ModuleApi and CaseApi instead of SugarApi and using their bean creation methods, but they just lack documentation to get anywhere, or don't work that way.

 

At the very minimum, I need a name and a parent account for a Case, as I understand, but this does not create a new case:

 

$case = BeanFactory::newBean('Cases');
$case->name = 'TEST CASE ' . rand();
if ($case->load_relationship('accounts')) {
  // The log message appears, so the relationship exists and was loaded
  $GLOBALS['log']->fatal('Relationship test');
  // Hardcode an existing ID just to test that it works first
  $case->accounts->add('c7a2b75e-1aa9-11e7-8618-0cc47a920094');
}
$GLOBALS['log']->fatal($case->name); // Works, I can see the name of the case
$relatedAccounts = $case->accounts->getBeans();
$GLOBALS['log']->fatal($relatedAccounts); // Returns an empty array
$GLOBALS['log']->fatal(print_r($relatedAccounts, true)); // Still an empty array
$case->save(); // If this line is not commented out, endpoint returns HTTP 500 error

return json_encode($case); // Otherwise this returns false

 

A Notes record only requires a name and it worked flawlessly, no errors after $note->save();. Why can I not add a relationship and a name to a new Case and save it?

 

Does it have something to do with the fact that the Cases module object key is aCase and object name is Case, different from other modules?

Outcomes