AnsweredAssumed Answered

Help with Logic Hook to Create a New Account Record with Opportunity fields copied over Once Sales Stage = Closed Won

Question asked by Jake Ashley on May 30, 2019
Latest reply on May 30, 2019 by Jake Ashley

Hi.

I have been racking my head over this, and need a little guidance. I apologize if this question has already been answered elsewhere. I cannot find a solution that works for me.

 

Our version:

OnDemand 9.0

 

The goal is to create an Account record once the opportunity sales stage = closed won. I can achieve this functionality with workflow management, however I cannot seem to use field variables from the opportunity module to populate in the new account record. I can only have that account created with a static name.

 

Our crm flow is:

Leads --> Convert to Prospects(opportunities module) and a contact. -->The prospect(opportunities) stays in the module until the sales stage is closed won. --> at that point the Prospect record is deleted and a Current Account(accounts module) record is created with that data copied over.

 

The logic hook I have tried is: 

 

Manifest:

<?php

$manifest = array(
    'acceptable_sugar_flavors' => array('PRO','CORP','ENT','ULT'),
    'acceptable_sugar_versions' => array(
        'exact_matches' => array(),
        'regex_matches' => array('9.0.*'),
    ),
    'author' => 'LSMG',
    'description' => 'Logic hook using after_save to create a current account when prospect sales stage = contract signed(closed won)',
    'icon' => '',
    'is_uninstallable' => true,
    'name' => 'Account Creation Automation - Using Extension Framework',
    'published_date' => '2019-05-29',
    'type' => 'module',
    'version' => '1.0',
);

$installdefs = array(
     'id' => 'Create Account',
     'copy' => array(
          0 => array(
               'from' => '<basepath>/Files/custom/modules/Opportunities/account_create.php',
               'to' => 'custom/modules/Opportunities/account_create.php',
          ),
     ),
     'hookdefs' => array(
          array(
               'from' => '<basepath>/Files/custom/Extension/Opportunities/Ext/LogicHooks/account_create_hook.php',
               'to_module' => 'Opportunities',
          )
     ),
);

 

account_create_hook.php

<?php

$hook_version = 1;
$hook_array = Array();

$hook_array['before_save'] = Array();
$hook_array['before_save'][] = Array(
    1,
    'Store Sales Stage Values',
    'custom/modules/Accounts/account_create.php',
    'Account_Create',
    'storeValues'
);

$hook_array['after_save'] = Array();
$hook_array['after_save'][] = Array(
    1,
    'Account Creation Automation',
    'custom/modules/Accounts/account_create.php',
    'Account_Create',
    'createAccount'
);

 

account_create.php

<?php

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

class Account_Create
{
     function storeValues($bean, $event, $arguments)
    {
        //store sales stage as a new bean property
        $bean->stored_fetched_row_c = $bean->fetched_row;
    }

    function createAccount($bean, $event, $arguments)
    {
        //check if the sales stage has reached Closed Won(Contract Signed) and create current account record if so
        if (isset($bean->stored_fetched_row_c) && $bean->sales_stage == 'Closed Won' && $bean->stored_fetched_row_c['sales_stage'] != $bean->sales_stage)
        {  
            $newAcc = BeanFactory::newBean('Accounts');
            $newAcc->id = create_guid(); 
            $newAcc->new_with_id = true; 
            $newAcc->save();
        }
    }
}

 

Do you see something I am missing or have done incorrectly?

 

Thanks in advance.

Outcomes