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
  • Can you provide some context? Why to delete an Opportunity record?

    What is the goal on creating an Account from an Opportunity?

    Regards

    André Lopes
    Lampada Global
    Skype: andre.lampada
  • 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.

Reply
  • 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.

Children
No Data