Create new project on Opportunity stage change via a logic hook

sugarcrmdevelopers —  January 8, 2013 — 18 Comments

Here’s a great tip from the forums, on detecting an opportunity stage change to trigger creating a new project record. You’ll want to create this as a ‘after_save’ logic hook…

And definitely check out this post to learn how to package and install this hook on your Sugar instance.

18 responses to Create new project on Opportunity stage change via a logic hook


    As John describes in lot of previous post I think it’s better to use BeanFactory to create a new bean with code “BeanFactory::getBean(‘Projects’)” instead of “new Project()”


    IMHO, it is better to create records in related modules in an after_save hook. Otherwise you run the risk creating widowed records if the initial save fails.


      I agree, but can you check to see if values changed now in an after_save? It used to be that the fetched_row wouldn’t contain the old values in after_save. So that is why so many examples exist that use before_save. It would be great if this isn’t the case anymore.


        I usually set a session variable dependent on the outcome in before_save and then refer to it in after_save, not forgetting to unset it afterwards. As a double check I always unset all session variables I am using , in before_save


          or json_encode the fetched_row array and pass it accordingly. Still it would be preferable to have the fetched row values in after_save as you mentioned 🙂


        What I usually do when I need these values in after_save is to create two logic hooks.

        One called saveFetchedRow: it’s the first hook to run in before_save and it grabs the value of $bean->fetched_row and sticks it into a temporary variable on $bean.

        Another called replaceFetchedRow: it’s also the first hook to run on after_save, and what it does it to replace the value of $bean->fetched_row by the value of the $bean’s temporary variable.


        You can have access to fetched_row in the after_save event. I updated the code example to reflect this.


          And there it is!


          Unfortunatly some values in fetched_rows are modified (

          I think the best approach is what @jcsmorais:disqus describes with 2 logic_hooks


            Can you report that as a bug @twitter-257643679:disqus ? Seems like the wrong behaviour to me…


              Does anybody know if this was reported as a bug and if there is a bugfix in git we could cherrypick?


              We are seeing similar issue where some values in the $_REQUEST are lost by time of Before_save logic hook, any thoughts?

              Mon Feb 18 16:42:31 2013 [15615][1][FATAL] request->beforeSave->name: Test
              Mon Feb 18 16:42:31 2013 [15615][1][FATAL] request->beforeSave->pr_start_time_c: 19/02/2013 11:00
              Mon Feb 18 16:42:31 2013 [15615][1][FATAL] Bean->beforeSave->pr_start_time_c:

              Mon Feb 18 16:42:31 2013 [15615][1][FATAL] Bean->afterSave->name: Test
              Mon Feb 18 16:42:31 2013 [15615][1][FATAL] Bean->afterSave->pr_start_time_c:


              description = $bean->pr_duplicateparentid_c;

              $GLOBALS[‘log’]->fatal(‘request->beforeSave->name: ‘ . $_REQUEST[‘name’]);

              $GLOBALS[‘log’]->fatal(‘request->beforeSave->pr_start_time_c: ‘ . $_REQUEST[‘pr_start_time_c’]);

              $GLOBALS[‘log’]->fatal(‘request->beforeSave->pr_stop_time_c: ‘ . $_REQUEST[‘pr_stop_time_c’]);

              $GLOBALS[‘log’]->fatal(‘Bean->beforeSave->pr_start_time_c: ‘ . $bean->pr_start_time_c);

              $GLOBALS[‘log’]->fatal(‘Bean->beforeSave->pr_stop_time_c: ‘ . $bean->pr_stop_time_c);

              global $db;



              $parent_id = $bean->pr_duplicateparentid_c;

              $id = $bean->id;

              $query = “INSERT INTO contacts_cases (id, contact_id,case_id,date_modified,deleted)”

              .” SELECT UUID(),contact_id,’$id’, NOW(),0 FROM contacts_cases WHERE case_id = ‘$parent_id'”;






              function afterSave(&$bean, $event, $arguments)


              $GLOBALS[‘log’]->fatal(‘Bean->afterSave->name: ‘ . $bean->name);

              $GLOBALS[‘log’]->fatal(‘Bean->afterSave->pr_start_time_c: ‘ . $bean->pr_start_time_c);

              $GLOBALS[‘log’]->fatal(‘Bean->afterSave->pr_stop_time_c: ‘ . $bean->pr_stop_time_c);





    Would be even better if this could be achieved using Workflow engine in Pro?


    I personally would have used loadBean(“Project”) but I just discovered that will be deprecated soon… so I better change my habits and start using “BeanFactory::getBean(‘Projects’)” instead!

Trackbacks and Pingbacks:

  1. Download a File in a Logic Hook | eggsurplus solutions - June 19, 2013

    […] To illustrate how this could be done here is a simple example that downloads a text file whenever a contact gets added or updated to have a title of CEO. First is the before_save logic hook that captures when a value gets changed. In the require_download function below we check to see if the title value has changed. If so, set a session variable to be used to both tell the next hook to execute and to pass data that could be used to retrieve data. Ideally, this should be done in an after_save logic hook to ensure that the save actually happened. Do it that way, but be aware that you may have issues with the fetched_row having updated values instead of the original values depending on which version of SugarCRM you are on. See the comments on this dev blog post for more information: SugarCRM Developers Blog: Create new project on Opportunity stage change via a logic hook. […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s