HOWTO: Detect record state in a logic hook

sugarcrmdevelopers —  February 14, 2011 — 8 Comments

The advice contained in this post is out of date.

One problem developers that are just getting started with SugarCRM seem to run into in the use of logic hooks. Logic hooks are a powerful mechanism to interject business logic in the places in the application where in is most often needed, such as when you retrieve and save record, login or logout of the application, or set or delete a relationship between records.

A common question that pops up on the forums revolves around record detection. Let’s say you have a logic hook where you only want the logic execute if the field has changed value. In this case, you’ll want to enclose your logic in an if block such as this ( thanks to davidboris on the forums for this tip ):

Another question is how do you specify logic that should only be executed on the initial record creation, but not on subsequent saves. You can use a similar technique to what is above, instead simplying checking if the fetched_row array has the id element set.

8 responses to HOWTO: Detect record state in a logic hook

  1. 

    May be I misunderstood a situation when the code above (gist #2) should fire but I think $bean->fetched_row array IS _empty_ on creating _new_ record. So thus code in the gist should work only when updating existing record. Am I wrong?

  2. 
    Shijin Krishna June 6, 2014 at 1:28 am

    Thanks for the tip..:-)

  3. 

    is this for a before_save or after_save logic hook or does it not matter ?

    • 

      If you want to see what changed then you need to use after_save logic hook. The full set of field changes would not be known until after the bean has been saved.

Trackbacks and Pingbacks:

  1. help with logic hook code please - it works just needs tweaking - February 12, 2013

    […] […]

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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