hooksugarcrmafter-save

SugarCRM identify if the record is created or updated from after_save hook


I have a SugarCRM 6.5 application. I have to trigger an email using after_save hook when a record is created(not during update).

I know I can use the workflow. But I can't use the workflow for this scenario due to multiple reasons.

Now I need to know how to find if the record is just created or is it being updated from after_save hook file?

if(!empty($bean->id) && date('Y-m-d',strtotime($bean->date_enterd)) == date('Y-m-d')) {

This condition won't help. Any idea how I can find if the record is created but not updated from after_save hook?


Solution

  • I achieved this by doing the following.

    1. Created a new field as "is_first_mail_sent" and set the default value as 'no'.
    2. In after_save hook I check if the value is 'no'. if(!empty($bean->id) && ($bean->is_first_mail_sent== 'no')) {
    3. If the condition satisfied, I send the email and update the is_first_mail_sent value to 'yes'.

    Note: I am not showing this value in layout. So no one can edit this value manually.

    This works perfectly for me. The condition will return false forever after the first after_save hook since I have updated the value.

    I am accepting this as an answer now. I am happy to change it if anyone else has another better idea.