Custom entry point and $bean->save() method crashing

Hello all, I had an issue with an entry point when trying to save a bean. I have tried disabling row level security and moving the save earlier and later within the code. For some reason, whenever I try to save the contact within the entry point it crashes the request and nothing happens.

This is my code:

<?php
if (!defined('sugarEntry') || !sugarEntry) die ('Not a valid Entry Point');

global $current_user;


date_default_timezone_set('America/New_York');

$call = BeanFactory::newBean('Calls');
$module = $_POST['module'];
$parent_id = $_POST['parent_id'];
$call_outcome_c = '';
//check if module is $oContacts
if ($module == 'Contacts') {
$contact_bean = BeanFactory::retrieveBean("Contacts", $parent_id);
$contact_bean->disable_row_level_security = true;

$target_list_id = $_REQUEST['target_list_id']; // get target list
$target_list = BeanFactory::retrieveBean("ProspectLists", $target_list_id);

$call->parent_type = "Contacts";
$call->parent_id = $parent_id;

if (!empty($_POST['description'])) {
$call->description = $_POST['description'];
}
//check if call outcome
if (isset($_POST['call_outcome_c'])) {
$call_outcome_c = $_POST['call_outcome_c'];

$call->call_outcome_c = $call_outcome_c;

$contact_bean->description .= "\n" . "\n" . $target_list->name . " - " . date('m/d/Y h:ia') . "\nCall Made by " . $current_user->full_name . " with outcome of " . $call_outcome_c . "\nDescription:" . $_POST['description'] . "\n";
//set contact call putcome to most recent call outcome
$contact_bean->call_outcome_c = $call_outcome_c;

$contact_bean->save(); // <<< This is the line of code in question, if I comment this out it will work correctly.
}
$call->assigned_user_id = $current_user->id;
$call->assigned_user_name = $current_user->full_name;
$call->direction = "Outbound";
$call->status = "Held";
$call->date_start = date('m/d/Y h:ia');


if($contact_bean->account_id != ""){
$contact_account = BeanFactory::retrieveBean("Accounts",$contact_bean->account_id);
if(isset($contact_account->name) && !empty($contact_account->name)){
$call->account_c = $contact_account->name;
}
}

//get prospect list bean and load relationship between it and campaigns
$target_list->load_relationship('atc_isscampaigns_prospectlists_1');
//pull campaign bean
$prospect_list_relation = $target_list->atc_isscampaigns_prospectlists_1->getBeans();
$campaign_bean = current($prospect_list_relation); //populate campaign bean to pull name

$call->campaign_name_c = $campaign_bean->name;

$call->direct_phone_c = $contact_bean->phone_other;

$call->name = "Made call to " . $contact_bean->full_name;
$call->save();

//Relate beans
$target_list->load_relationship('prospectlists_calls_1');
$target_list->prospectlists_calls_1->add($call->id);

$campaign_bean->load_relationship('atc_isscampaigns_calls_1');
$campaign_bean->atc_isscampaigns_calls_1->add($call->id);

//relate call to contacts using custom fields because regular relation doesn't work

$columns = array("call_id" => $call->id, "contact_id" => $parent_id);
$values = array($call->id, $parent_id);
$call->set_relationship('calls_contacts', $columns, true, false, $values);
}
//value to return to call_logging.js
$ret = array('message' => $msg, 'row_id' => $parent_id, 'call_outcome' => $call_outcome_c, 'module' => $module);
echo json_encode($ret);

?>

  • Initially consider migrating your entrPoints into endpoints once entryPoints will be deprecated in a future release.

    Double check sugarcrm.log and apache / php error_log. Hopefully you will see some valuable insight.

    By reading your code I couldn't notice anything which explain the issue, so analyze those log files and let us know.

    Regards

    André Lopes
    Lampada Global
    Skype: andre.lampada
  • Have you checked your PHP and Sugar Logs? Often there is information there that can help.

    As for your code, a few observations:

    I've not seen this way of disabling row level security before, so I don't know if it works.

    $contact_bean = BeanFactory::retrieveBean("Contacts", $parent_id);
    $contact_bean->disable_row_level_security = true;

    I would suggest trying the following as specified in the Bean Factory documentation:

    https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_8.3/Data_Framework/Models/BeanFactory/ 

    $bean = BeanFactory::retrieveBean($module, $record_id, array('disable_row_level_security' => true));

    Also, after every retrieve I like to check if I actually got a bean back, the retrieveBean returns false if I didn't get one (which I why I never use getBean to retrieve an existing id (it would return an empty bean if the id was not valid):

    if(!empty($bean)){
      //yep, I got a bean!
    }

    Having said all this, Entry Points are set to be deprecated, I would suggest using Endpoints (Rest API) instead.

    There are a number of ready-made End Points that you can use to retrieve Contact and Account information and create Calls or if those don't work for you, you can create your own:

    https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_8.3/Integration/Web_Services/REST_API/E… 

    Hope this helps,

    Francesca

    As a side note, it is easier to read formatted code. You can do that by going to the "More" in the editor and selecting Syntax Highlighter. This opens a window where you can paste your code. Don't forget to select the type of code from the dropdown at the top.