sugarcrmsuitecrmbefore-saveafter-save

Difference between before_save and after_save using real time entities


I want to know the exact difference between before_save and after_save. Now, I have read the documentation and I know the difference as per their name. But, I want to know the exact difference using real time entities. It would be great if you provide any example.


Solution

  • Before Save: The functionality written here will be called when you hit the save button and before the record is stored in the database.

    Usage: Before save can be used normally. For a simple eg, lets say we can modify or add a field value just before the record goes into the database.


    After Save: The functionality written here will be called when you hit the save button and after the record is stored in the database.

    Usage: To help you understand the use of after_save,
    Lets take a scenario where we have Student record containing SL.No, Name, Course and Student No.
    Lets say the SL.No is an auto incrementing field and Student no is the combination of the first letter of the Course and the SL.No.
    Now here the auto incremented no wont exist untill the record is saved in the database, hence you wont get the required Student No unless the record is saved.
    So after_save here helps since the logic is executed after the record has been saved, to which an auto incremented no is already generated.