1

I have some query on Javascript remote action,as i am not much aware of that concept.Pls help me out..I need to access a parameter of Remote Action method into another remote action method

Global class PolicyHolderDetails {
Public string Name{get;set;}
@RemoteAction
global static PageReference Save(String Name){
// some stuff.
return null;

}

@RemoteAction
 global static PageReference CheckInsurance(String insurance1){
  //i need access to Name parameter here..So that i can query..Is there any workaround for this..

}
  • static method can only access static variables – Santanu Boral Aug 17 '17 at 3:49
  • even though i declare the variable as static it is giving error.And first of all how should i access that in 2nd method.I tried calling Save method but there are more parameters other than Name so that wont be good idea.. – Deepak Agarwal Aug 17 '17 at 3:55
  • You get it from the same place you got it when calling the first method – Eric Aug 17 '17 at 4:02
  • yes true but save method has around 10 parameters i need to call them with all..Is there any alternative?? – Deepak Agarwal Aug 17 '17 at 4:12
  • May you add an inner class, then define many Attributes in it. Then you can pass a object as the parameter from visualforce page. Does it make sense? – daniel wu Aug 17 '17 at 6:16
1

RemoteAction methods are static, and can't store Values. In remote action each transaction in Sales-force starts as a fresh slate, with nothing loaded in memory.

    Global class PolicyHolderDetails {

    public class Member {
        Public string Name{get;set;}
        // declare your Member here 
        Public Member(){}
    }

    @RemoteAction global static Member Save(String Name){
    // some stuff.
    Member member=new Member();
    member.Name= Name;
    return Member; // Store the Member in visual Force page.
    }

   // Resend member again from visual force Page to your Second remote action
    @RemoteAction global static PageReference CheckInsurance(String insurance1,Member member) {
      // Use Member here     
    }

OR

Session Class

Another Alternative way is keep in server Cache. Way of doing this would be to use the Platform Cache to store the data serverside. You can use a Session Cache to store the required data between Javascript Remoting calls and get it from that same cache when you need it.

Use the Cache.Session class to add, retrieve, and manage values in the session cache. The session cache is active as long as the user’s Salesforce session is valid (the user is logged in, and the session is not expired).

Global class PolicyHolderDetails {
 public class Member {
     Public string Name{get;set;}
     // declare your Member here 
     Public Member(){}
     }
@RemoteAction global static PageReference Save(String Name){
// some stuff.
 Member member=new Member();
 member.Name= Name;
 // Add Member to the cache.
 Cache.Session.put('member', member);
return null;  
}

@RemoteAction global static PageReference CheckInsurance(String insurance1){
   // Return cached value whose type is the inner class Member.
   Member member= (Member)Cache.Session.get('member');
}

Above session cache code only works if your created new Platform Cache. If you don't know how create a Platform Cache, Follow below link one by one

First Follow this link to activate Request Trial Capacity

Second Follow this link to create new platform cache

If your finished with above two step then click on edit on platform cache which your newly created, Change trail value 0 to 5 in Session Cache Allocation and Org Cache Allocation and save it.

Your done.

How to use session cache

| improve this answer | |
  • I preferred to work on solution2 but there is an error..IN line Cache.session.put('member',Member); Error:Member doesnot exist.. – Deepak Agarwal Aug 18 '17 at 6:47
  • i have updated to ' Cache.Session.put('member', member); ' may be the reason of case sensitive. can u try now . – NITHESH K Aug 18 '17 at 6:54
  • Yes it is saving..But no data is getting created..cache.Session.SessionCacheException: A default partition was not found.This is the error i am getting bro – Deepak Agarwal Aug 18 '17 at 7:25
  • You have to create Platform Cache for that . – NITHESH K Aug 18 '17 at 8:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.