How to change the relationship of a record from one module to another

Sugar On Demand | 9.1 | Professional

Our Flow & Some Background Info:

Unclaimed Lead(Leads Module) -> Prospect(Opportunities Module) -> Current Account(Accounts Module)

Our marketing department uploads pre-qualified leads to our 'Unclaimed Leads' module. Our sales team(users) then  pick and choose leads and convert them to a Prospect to begin the sales cycle. When the lead is converted, the user creates a contact record and a prospect record that are related. Once the sales stage is Closed Won, the prospect then converts to a current account via a logic hook.

My Question:

Everything is currently working as it should, however the issue I am running into is once a Prospect converts to a Current Account, how do I take the contact that was linked to the Prospect record and link it to the new Current Account record?

Thanks in advance for pointing me in the right direction.

Parents
  • all you need to make a "after save" logic hook which will do rest of job. focus bean will be Prospect

    1) Check if previous prospect status was not "Converted" and current status is, it is by using $bean->fetched_row.

    2) If above condition met, then Pull contact_id & account_id of current bean.

    3) Load account bean using account_id and use load_relationship function to retrieve it's contact relationship link.

    4) just use function "add" to attach contact_id with account bean.

    Little suggesion on flow.

    Usually, Sugar assumes the first stage of the record is Prospect, then Lead and then final conversion to set of Account + Contact + Sale.

    Not sure how you managing it but this is what I learned over years of practice.

Reply
  • all you need to make a "after save" logic hook which will do rest of job. focus bean will be Prospect

    1) Check if previous prospect status was not "Converted" and current status is, it is by using $bean->fetched_row.

    2) If above condition met, then Pull contact_id & account_id of current bean.

    3) Load account bean using account_id and use load_relationship function to retrieve it's contact relationship link.

    4) just use function "add" to attach contact_id with account bean.

    Little suggesion on flow.

    Usually, Sugar assumes the first stage of the record is Prospect, then Lead and then final conversion to set of Account + Contact + Sale.

    Not sure how you managing it but this is what I learned over years of practice.

Children
  • Thanks Ashish Dwivedi

    I am still learning best practices with Sugar, but this is my code below. Can you (or anyone else) give me some advice or help me correct this? Thanks.

    <?php

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

    class CreateAccountHook
    {

        function after_save_method($bean, $event, $arguments)
        {
            //check if the sales stage value has changed and reached contract signed
            if(isset($arguments['dataChanges']['sales_stage']) && $bean->sales_stage=='Closed Won')
            {  
                //create a new account record from prospect(opportunity) if conditions are met
                $newAcc = BeanFactory::newBean('Accounts');

                //load relationship of prospect(opportunity) to contact and add to the new account record
                $rel_name = 'opportunities_contacts_1';
                $bean->load_relationship($rel_name);
                $relatedBeans = $bean->$rel_name->getBeans();
                $newAcc->$rel_name->add($bean->id);

                //copy the name from prospect to the new current account record           
                $newAcc->name = $bean->name;
                $newAcc->save();
            }
        }
    }

    ?>