Run a logic hook only on new records

Hi,

I’m using SuiteCRM 7.7.8.

I wrote a logic hook to run on ‘before_save’ but I’m confused on how I can tell if a new record (in my case Opportunity) is being created or if an existing record is being updated. I only want my logic hook to run for newly created records.

Did some Googling and saw that SugarCRM uses a flag “isUpdate” in the logic hooks that is true or false for a new record.

How can one find this information for Suite CRM?

Hi,
You can use the fetched_row flag to determine this.

//Only execute on new rows
if ($bean->fetched_row == false) {
//Proceed …

Dianna

3 Likes

Great! That worked! Thanks a lot.

Another question: How can you tell which fields are being updated on a before_save or after_save?

To determine is a field has been changed, you would compare its current value to its fetched row value. Here is an example where I see if the sales stage has been modified…

      if  ($bean->sales_stage != $bean->fetched_row['sales_stage']) {

If you need to check an entire list of fields, you would probably want to put the field names in an array and loop through them as I am doing here:

       $shared_fields = array('proposal_number_c','auto_inquiry_number_c','amount','option_value_c',
                                'priority_c','urgency_ranking_c','prob_go_c','prob_win_c',
                                'contact_id_c','contact_id1_c','contact_id2_c',
                                'contact_id5_c','user_id_c','user_id1_c','required_install_date_c');
        // Then cycle through them
        foreach($shared_fields as $shared_field){
           // do a change detection leveraging the fetched_row value
           if($bean->$shared_field != $bean->fetched_row[$shared_field]) {
              .
              .
              .
           }
        }

Good luck!
Dianna

2 Likes

Hello Dianna,
How can I check user creating new record or update record in JS?

1 Like