Skip to content

Instantly share code, notes, and snippets.

@SugarCRMExamples
Created March 19, 2014 16:09
Show Gist options
  • Save SugarCRMExamples/9645137 to your computer and use it in GitHub Desktop.
Save SugarCRMExamples/9645137 to your computer and use it in GitHub Desktop.
<?php
require_once('modules/Contacts/ContactsApiHelper.php');
// Since the ContactsApiHelper exists, we'll extend it
// If it didn't we would just extend the SugarBeanApiHelper
class CustomContactsApiHelper extends ContactsApiHelper
{
// Mimic the SugarBeanApiHelper->formatForApi() class
public function formatForApi(SugarBean $bean, array $fieldList = array(), array $options = array())
{
// Same as before, snipped for display purposes.
}
// Mimic SugarBeanApiHelper->populateFromApi()
public function populateFromApi(SugarBean $bean, array $submittedData, array $options = array() )
{
$errors = parent::populateFromApi($bean, $submittedData, $options);
if ($errors !== true || empty($submittedData['top_opp']['id'])) {
// There were errors in the original, don't bother saving the opp
// Or they aren't trying to save the opp, so just pass back the parent
return $errors;
}
// Do a full retrieve so the logic hooks on save have everything they need
$opp = BeanFactory::getBean('Opportunities', $submittedData['top_opp']['id']);
// Load up the ApiHelper class for Opportunities and have it populate from
// this subsection of data.
$oppErrors = ApiHelper::getHelper($GLOBALS['service'], $opp)
->populateFromApi($opp, $submittedData['top_opp']);
if ( $oppErrors !== true ) {
// Errors populating the opportunity
return $oppErrors;
}
// Typically the save happens after this is all done, but since that function doesn't
// know about this extra opportunity, we'll just save it here.
$opp->save();
// Checked both the parent errors, and the opp errors, they are both true so return true
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment