SugarCRM Developers on Twitter

HOWTO: Detect record state in a logic hook

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.

Like this:

Related

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

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?