AnsweredAssumed Answered

Custom module logic hook to create Opportunity fails to link Account

Question asked by insxch on Aug 27, 2017
Latest reply on Aug 31, 2017 by insxch

I have a custom module (Named QTES_Quotes) that needs to create an Opportunity record, transfer the field values and relationships, and save the record. I've gotten everything to work except for adding the stock relationship to the Accounts module (renamed as Agencies).

 

The custom module is named QTES_Quotes. Reading down the code posted below, I've done the following:

  • Load the relationship between the QTES_Quote and the Opportunity, and retrieve the assigned record
  • Load the relationship between QTES_Quotes and the Agent (renamed Contacts module), and retrieve the assigned record
  • Load a second relationship between QTES_Quotes and the Agent (renamed Contacts module), and retrieve the assigned record
  • Load the relationship between QTES_Quotes and the GRPS_Groups (custom module), and retrieve the assigned record
  • Load the relationship between QTES_Quotes and the Agencies (renamed Accounts module), and retrieve the assigned record
  • Create a new Opportunity bean
  • Load the relationship between the Opportunity and the Agent (renamed Contacts module)
  • Load a second relationship between the Opportunity and the Agent (renamed Contacts module)
  • Load the relationship between the Opportunity and the GRPS_Groups (custom module)
  • Load the relationship between the Opportunity and the Agencies (renamed Accounts module)
  • If the QTES_Quote status field is changed to 'Complete' and saved:
    • Transfer the three fields from the QTES_Quote bean to the new Opportunity bean
    • Save the new Opportunity
    • Add the new Opportunity record to the existing QTES_Quote record
    • On the new Opportunity bean, add the new Agent (renamed Contacts module) relationship
    • If needed, on the new Opportunity bean, add the new Assistant (renamed Contacts module) relationship
    • On the new Opportunity bean, add the new GRPS_Groups (custom module) relationship
    • On the new Opportunity bean, add the new Agencies (renamed Accounts module) relationship
      • The script will fail here with a: PHP Fatal error: Call to a member function add() on null

 

I've tried creating a new Accounts bean with BeanFactory, searching for the name using a SugarQuery, retrieving the Agency, loading the relationship (accounts_opportunities) and attaching the new Opportunity that way. It still errors out with the same error message as above.

 

Any help would be really appreciated. Thank you.

 

 

before_save Logic Hook  in my custom module:

if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

//Import SugarQuery class require_once('include/SugarQuery/SugarQuery.php');

class QTES_Quotes_Hooks {

   public function createNewOpportunityFromQuote($bean, $event, $arguments) {

      $quoteOpportunityRelationship = 'opportunities_qtes_quotes_1';       $bean->load_relationship($quoteOpportunityRelationship);

      $quoteAgentRelationship = 'contacts_qtes_quotes_1';

      $bean->load_relationship($quoteAgentRelationship); $quoteRelatedAgent = $bean->$quoteAgentRelationship->getBeans();

      reset($quoteRelatedAgent);

      $quoteAssignedAgent = current($quoteRelatedAgent);

      $quoteAssistantRelationship = 'contacts_qtes_quotes_2';       $bean->load_relationship($quoteAssistantRelationship);

      $quoteRelatedAssistant = $bean->$quoteAssistantRelationship->getBeans();

      reset($quoteRelatedAssistant);

      $quoteAssignedAssistant = current($quoteRelatedAssistant);

      

      $quoteGroupRelationship = 'grps_groups_qtes_quotes_1'; $bean->load_relationship($quoteGroupRelationship);

      $quoteRelatedGroup = $bean->$quoteGroupRelationship->getBeans();

       reset($quoteRelatedGroup);

      $quoteAssignedGroup = current($quoteRelatedGroup);

      $quoteAgencyRelationship = 'accounts_qtes_quotes_1';

       $bean->load_relationship($quoteAgencyRelationship);

      $quoteRelatedAgency = $bean->$quoteAgencyRelationship->getBeans();

       reset($quoteRelatedAgency);

      $quoteAssignedAgency = current($quoteRelatedAgency);

      $newOpportunityBean = BeanFactory::newBean('Opportunities');

      $opportunityAgentRelationship = 'contacts_opportunities_1';

      $newOpportunityBean->load_relationship($opportunityAgentRelationship);

      $opportunityAssistantRelationship = 'contacts_opportunities_2';

      $newOpportunityBean->load_relationship($opportunityAssistantRelationship);

      $opportunityGroupRelationship = 'grps_groups_opportunities_1';

      $newOpportunityBean->load_relationship($opportunityGroupRelationship);

      $opportunityAgencyRelationship = 'accounts_opportunities';

      $newOpportunityBean->load_relationship($opportunityAgencyRelationship);

      switch ($bean->quote_status_c) {

      case "Complete":

         $newOpportunityBean->requested_effective_date_c = $bean->requested_effective_date_c;

         $newOpportunityBean->number_of_employees_c = $bean->number_of_employees_c;

         $newOpportunityBean->business_segment = $bean->quote_segment_c;

         $newOpportunityBean->save();

         $bean->$quoteOpportunityRelationship->add($newOpportunityBean->id);

         $newOpportunityBean->$opportunityAgentRelationship->add($quoteAssignedAgent->id);

         if (!empty($quoteAssignedAssistant->id)) {

            $newOpportunityBean->$opportunityAssistantRelationship->add($quoteAssignedAssistant->id); }

         $newOpportunityBean->$opportunityGroupRelationship->add($quoteAssignedGroup->id);

         $newOpportunityBean->$opportunityAgencyRelationship->add($quoteAssignedAgency->id);

      break;

      }

   }

}

?>

Outcomes