Need help adding line to logic hook that will copy all data to a new module

Sugar OnDemand 9.1.0 Professional

When an opportunity reaches a sales stage of Closed Won, we create an account record in the Accounts module. The below works as intended in that respect, but I want to ensure that all data from the Opportunity record is copied over to the new account record and then deletes the opportunity record. Can you help me achieve this?

<?php

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

class CreateAccountHook
{

    function after_save_method($bean, $event, $arguments)
    {
        //check if a fields value has changed
        if(isset($arguments['dataChanges']['sales_stage']) && $bean->sales_stage=='Closed Won')
        {
               $newAcc = BeanFactory::newBean('Accounts');
            $newAcc->name = $bean->name;
            $newAcc->save();
        }
    }
}

?>
Parents Reply Children
  • Thanks for asking. Our process is as such:

    Marketing provides a list of leads that meet our requirements. Our sales staff(users) can then browse those leads and claim(convert) them according to region, etc, which then converts them to a Prospect(Opportunity). When they are a prospect, they are in the sales cycle and once the sales cycle = closed won, that prospect record then becomes a current account. Our goal is to copy all data from that prospect record to a new account record. Hope that helps clarify.

  • You can use this code/function to achieve what you are looking for.

    function outright_copy_bean($old_bean,$new_module){
    $new_bean = new $new_module;
    foreach($new_bean->field_defs as $key => $value){
    if(in_array($key, array("id", "date_entered"))){
    continue;
    }
    if($value["type"] == "link"){
    continue;
    }
    $new_bean->$key = $old_bean->$key;
    }
    $new_id = $new_bean->save();
    return $new_id ? $new_id:false;
    }

    However, regarding your original question, There is no good sense of converting opportunity to Account.

    Opportunity is "Sales" type entity and Account is "Company", so they are dissimilar entities, which means they have separate type of fields, so not eligibile for copying bean.

    What sugar expects is a conversion cycle from Target->Lead, then Lead->Account ( with opportunity).

    if above code does not work for on-cloud, please considering changing your variables to fixed names of module and bean.