duplicate record automation

Hi There,

What is the best way to duplicate a record on condition?

What I want to do is when an opportunity status is Closed Won & a custom field (job_status - pre-updated by a workflow linked to a custom module) is Complete, the opportunity is duplicated (with a few alterations e.g. expected close date, name etc...). I have played about in process author but there seems to be too many restrictions in there (e.g. cant copy relate field info, dropdown info etc...). I am assuming it will be a logic hook but I'm not entirely sure where to start or whether it is at all possible.

Any help would be greatly appreciated, thanks in advance!

SV

I am using on demand version 7.6.1 Enterprise

  • Hi SV,

    I would use the following before_save and after_save logic hooks:

    <?php
    if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
    
    class Opportunity_Logic_Hooks {
        function before_save_method($bean, $event, $arguments) {
            $bean->stored_fetched_row_c = $bean->fetched_row;
        }
    
        function after_save_method($bean, $event, $arguments) {
            if (isset($bean->stored_fetched_row_c) && $bean->sales_stage == 'Closed Won' && $bean->job_status == 'Complete' && ($bean->stored_fetched_row_c['sales_stage'] != $bean->sales_stage || $bean->stored_fetched_row_c['job_status'] != $bean->job_status)) {
                $new_opp = BeanFactory::getBean('Opportunities', $bean->id);
                $new_opp->id = create_guid();
                $new_opp->new_with_id = true;
                $new_opp->sales_stage = 'New';
                $new_opp->job_status = 'New';
                $new_opp->save();
            }
        }
    }
    

    More information on logic hooks can be found here, and about using the SugarBean object here. Let me know if you have any questions!

    -Alan

  • Hi Alan,

    This worked brilliantly, thank you so much for your help, it was greatly appreciated!

    Regards,

    SV