Activity Forums Salesforce® Discussions What is the scope of static variable in Apex?

  • shariq

    Member
    September 12, 2018 at 1:45 pm

    Hi,

    A static variable is static only within the scope of the Apex transaction. It's not static across the server or the entire organization. The value of a static variable persists within the context of a single transaction and is reset across transaction boundaries.

    Hope this helps!

  • Parul

    Member
    September 12, 2018 at 1:54 pm

    Hi,

    I think the scope for a Static variable in Apex is the entire transaction. Any static variables will maintain their value across that one transaction.

     

    Thanks.

  • chanchal kumar

    Member
    September 12, 2018 at 2:11 pm

    Hello Anurag,

    A static variable defined in a trigger doesn’t retain its value between different trigger contexts within the same transaction, such as between before insert and after insert invocations. Instead, define the static variables in a class so that the trigger can access these class member variables and check their static values.

  • Avnish Yadav

    Member
    September 13, 2018 at 12:07 pm

    Hello Anurag,

    Also on Async call, the static variable will not work.

    Thanks.

Log In to reply.

Popular Salesforce Blogs