0

I have static variables declared outside class and i am instantiating the variables in one method and access the same value in other method.But in spite of variable being set to static its still giving me null.

Public Class PolicyHolderDetails { 

Public Static ID PolId{get;set;}
Public Static ID Nameid{get;set;}
Public void CheckInsurance(){

Policy__c Pid=[select id from policy__c where Policy_Name__c='Health Insurance'][0];
     PolId=Pid.id;//setting the values..
     system.debug(PolId);
}
Public void Save(){ 
Account aa=[Select id from Account where name=:Name][0];
 Nameid=aa.id;
 system.debug(Nameid);
}
Public PageReference SaveAgreement() {
PolicyHolderDetails pobj=new PolicyHolderDetails();
//pobj.CheckInsurance();
//pobj.Save();
id pid=PolicyHolderDetails.PolId;//accessing the variables here..
id Nid=PolicyHolderDetails.Nameid;
}
}
  • I hope you are calling CheckInsurance() & save() method before trying to get values from Static variable. I see those commented in your code. – thatherahere Aug 21 '17 at 7:08
  • No..because it is a static variable i am directly calling from class name. – Deepak Agarwal Aug 21 '17 at 7:13
  • are you sure your SaveAgreement is executing only after value is assigned to your static variable? – salesforce Developer Aug 21 '17 at 7:16
  • @DeepakAgarwal - You are calling static variables correctly but before calling them you are have to assign values to those variables. As I can see in your code you are populating values in these static variables in CheckInsurance() & save() methods so If you want to get values from these static variables your method calling sequence should me like: PolicyHolderDetails obj = new PolicyHolderDetails(); // Call these methods first from somewhere so that values can be assgined to static variables. obj.CheckInsurance(); obj.Save(); // Now try to get values. obj.SaveAgreement(); – thatherahere Aug 21 '17 at 7:21
  • but those variables are declared static so no need to call them through method().Static variables has property that their values remain same through out the class – Deepak Agarwal Aug 21 '17 at 7:31
1

When a page invokes a method the view state - the values of the fields of the class - are automatically re-populated immediately before the method runs. But static variables are not part of that mechanism (by design). Removing Static from the declarations and referencing them as normal non-static variables in SaveAgreement should address the problem.

More detail in e.g. An Introduction to Visualforce View State.

PS

The simplest solution is to just do everything in the SaveAgreement method:

Public PageReference SaveAgreement() {

   Policy__c Pid=[select id from policy__c where Policy_Name__c='Health Insurance'][0];
   Id PolId = Pid.id;
   Account aa = [Select id from Account where name=:Name][0];
   Id Nameid = aa.id;

   ... just use the PolId and Nameid local variables here ...
}

but if you need to keep the logic spread across the 3 mthods:

Public Class PolicyHolderDetails { 

    Public ID PolId{get;set;}
    Public ID NameId{get;set;}

    Public void CheckInsurance(){
        Policy__c Pid=[select id from policy__c where Policy_Name__c='Health Insurance'][0];
        PolId = Pid.id;
    }

    Public void Save(){ 
        Account aa=[Select id from Account where name=:Name][0];
        NameId = aa.id;
    }

    Public PageReference SaveAgreement() {
        ... reference the PolId and NameId fields here ...
    }
}
  • I even tried it without static by creating object and accessing them..Still iam getting null. – Deepak Agarwal Aug 21 '17 at 7:37
  • @DeepakAgarwal Don't have them static. Hard to say what is going on otherwise - the methods of your controller aren't too clear and you haven't explained much about the page and what order those methods are called in. – Keith C Aug 21 '17 at 7:50
  • Ok let me explain you.Actually the code is very big so i have not kept everything​..So i have declared 2 variables.And the var are getting instantiated in CheckInsurance() and save() method..Now i need to access those values inside 3rd method Saveagreement ().For keeping values same throughout the class i used static..If there is any other option pls suggest me..i even tried without static...The order they are called are CheckIns(),Save() & Saveagreement () – Deepak Agarwal Aug 21 '17 at 8:31
  • @DeepakAgarwal Non-static is the way to go. Read the link I included in the answer and create a simple page/controller example to see that the mechanism works and then go back to your more complicated example and debug it. – Keith C Aug 21 '17 at 9:29
  • Pls can you help me with code actually its something urgent and i have tried everything... – Deepak Agarwal Aug 21 '17 at 11:18

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.