I am trying to access the returned values of a global class method and store them in a string. The string is declared as global static string. The value gets stored in the string when in the method. But when I try use the string in other method of same class, the string is blank/null. There are no values in it.

global with sharing class controllerc
      global static string sentresponse;
      global static Casewrapper createNewCase() 
       Casewrapper response= new Casewrapper ();
       system.debug('^^Sentreposne'+sentresponse); // here the string has returned values.
       return response;
  global static void ErrorMesage()
    Attachment attachres=new Attachment();
    system.debug('++response' + sentresponse); // here the string is null
    insert attachres;

Please help in how to fetch the response and call it in other method of the same class and assign it to the body of attachment. Thanks in advance.

Regards, Sfdcdev

  • 1
    Static can't store Values , each transaction in Sales-force starts as a fresh slate, with nothing loaded in memory.
    Nov 20, 2017 at 8:51
  • Or Another Alternative way is keep in server Cache. Way of doing this would be to use the Platform Cache to store the data serverside.
    Nov 20, 2017 at 9:32

1 Answer 1


To expand on Nitesh's comment, a call to createNewCase and a call to ErrorMesage are independent. This allows a request to be processed by any server and so greatly helps scalability and simplifies failover. Every requests is starting from scratch as far as the state in code variables (including static ones) is concerned.

So your choices are:

  • Perform both operations in a single method
  • Return the value required by the second method as part of the results of the first method and then pass the value back as an argument to the second method
  • I am not so sure about this , can we use salesforce platform cache in rest Api?
    Nov 20, 2017 at 9:34
  • 1
    @NITHESHK Perhaps, but that would be a complicated solution to a problem that could be solved more simply.
    – Keith C
    Nov 20, 2017 at 9:50
  • Hi KeithC and Nithesh .Thanks for the reply. As Keith said, I tried to return the response by saving it in a string and passing that string as a parameter in the method call. Still the value is null.
    – sfdcdev
    Nov 20, 2017 at 10:17
  • Just FYI, I am calling the ErrorMesage() method in catch block of createNewCase() method itself. But as you guys said, static cannot store values, what would be the best approach that can be used here to store and use the method's returned value in other method.
    – sfdcdev
    Nov 20, 2017 at 10:18
  • Performed both the operations in Single method.
    – sfdcdev
    Nov 23, 2017 at 11:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .