Activity Forums Salesforce® Discussions Does trigger.oldmap change during recursion in Salesforce?

  • Prachi

    Member
    August 16, 2018 at 1:24 pm

    Hi Madhulika,

    Trigger.old contains a version of the objects before the specific update that fired the trigger. However, there is an exception. When a record is updated and subsequently triggers a workflow rule field update, Trigger.old in the last update trigger won’t contain the version of the object immediately prior to the workflow update, but the object before the initial update was made. For example, suppose an existing record has a number field with an initial value of 1. A user updates this field to 10, and a workflow rule field update fires and increments it to 11. In the update trigger that fires after the workflow field update, the field value of the object obtained from Trigger.old is the original value of 1, rather than 10, as would typically be the case.
    Based on that, I think Trigger.old will maintain the original value even throughout multiple loops through the order of execution due to recursion

    thanks.

  • Parul

    Member
    September 14, 2018 at 4:29 am

    Hi

    Trigger.old contains a version of the objects before the specific update that fired the trigger. However, there is an exception. When a record is updated and subsequently triggers a workflow rule field update, Trigger.old in the last update trigger won’t contain the version of the object immediately prior to the workflow update, but the object before the initial update was made. For example, suppose an existing record has a number field with an initial value of 1. A user updates this field to 10, and a workflow rule field update fires and increments it to 11. In the update trigger that fires after the workflow field update, the field value of the object obtained from Trigger.old is the original value of 1, rather than 10, as would typically be the case.
    Based on that, I think Trigger.old will maintain the original value even throughout multiple loops through the order of execution due to recursion

  • shariq

    Member
    September 15, 2018 at 5:10 pm

    Hi,

    To explain more -

    Let me explain it with an example. Let say you have a requirement where you want to save the previoues owner of an account when the account owner is changed. You understand that you need to create a  trigger on update for account. Let say you click edit button and the owner is abc. Now you need to check that if owner is changed whehter it is defferent from previuos one.Here comes the need of trigger.oldmap. You check that if the  new entered owner let say xyz is same of diffrent from the previous owner. You can do that trigger.oldmap.account.owner(abc)== trigger.newmap.account.owner (xyz)..(just an example as this is not the right way to access account owner field).

     

    In the above example, you can make out that oldmap and newmap is used to compare the  old and new versions of Sobject records.

    Hope this helps.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos