Activity Forums Salesforce® Discussions What are the various considerations while implementing triggers in Salesforce?

  • shradha jain

    Member
    August 20, 2018 at 7:17 am

    Hello Suniti,

    Various  Considerations while implementing triggers in Salesforce are:

    • upsert triggers fire both before and after insert or before and after update triggers as appropriate.
    • merge triggers fire both before and after delete for the losing records, and both before and after update triggers for the winning record. See Triggers and Merge Statements.
    • Triggers that execute after a record has been undeleted only work with specific objects. See Triggers and Recovered Records.
    • Field history is not recorded until the end of a trigger. If you query field history in a trigger, you don’t see any history for the current transaction.
    • Field history tracking honors the permissions of the current user. If the current user doesn’t have permission to directly edit an object or field, but the user activates a trigger that changes an object or field with history tracking enabled, no history of the change is recorded.
    • Callouts must be made asynchronously from a trigger so that the trigger process isn’t blocked while waiting for the external service's response. The asynchronous callout is made in a background process, and the response is received when the external service returns it. To make an asynchronous callout, use asynchronous Apex such as a future method. See Invoking Callouts Using Apex for more information.
    • In API version 20.0 and earlier, if a Bulk API request causes a trigger to fire, each chunk of 200 records for the trigger to process is split into chunks of 100 records. In Salesforce API version 21.0 and later, no further splits of API chunks occur. If a Bulk API request causes a trigger to fire multiple times for chunks of 200 records, governor limits are reset between these trigger invocations for the same HTTP request.

    Thanks.

  • Parul

    Member
    September 15, 2018 at 4:42 am

    The various considerations while implementing triggers in Salesforce:

    • Upsert trigger fires on 4 different events :- before(insert, update), after (insert, update)
    • Merge trigger are fired on both events on delete
    • Field history is updated after the trigger has successfully finished processing data.
    • Any callout should be asynchronous so that trigger does not have to wait for the response.
    • A trigger cannot have a static keyword in its code.
    • If a trigger completes successfully the changes are committed to the database and if it fails the transaction is rolled back.

    Thanks

  • shariq

    Member
    September 15, 2018 at 5:22 pm

    Hi,

    Please find the realtime scenario where there is a chance of recursive trigger occurs.

    For example -

    We have a Location object with Active as a field which will maintain whether the Location is a Active or not.

    Scenario 1:

    If the user is inserting a new Location with Active flag is true, it will check all the existing active Location with the matching name and try to de-activate them.

    Scenario 2:
    If the user is update the existing Location with Active flag is true, it will check all the existing active Location with the matching name and try to de-activate them.

    Here we have to write a Trigger on the Location object with after insert, after update as a events.

    Possible issues:

    If we are not adding the conditions properly then there is a chance of getting into recrussive calls. Hence we have to follow below steps:

    (1) Make sure that all the possible conditions are added properly so that it will not recursively.

    (2) At times we may need to use Static variable to control the recursive calls.

    Note: This is one of the possible reasons and there may be multiple other reasons where the recursive trigger execution happens.

    Here is another relatime scenario for you:

    Whenever the user updates an Account, also update the corresponding Contacts. At the same time, if user is touching the Contacts, update the Accounts.

    Here also highly possibility of recursive trigger execution happens.

    Note: I could have copied lot of best practices but, explaining with the realtime scenarios will help you understand in a better way, hence providing all these points. But don't forget to follow all the possible best practices whenever you are writing Apex Code / Visualforce Page.

    Hope this helps.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos