3

VF Page component class Not getting value of soID.query works if i pass hard coded ID.Please someone tell me where i am wrong..**The constructor gets called first, before any variables are set. I dont know how to do this...

public with sharing class saleslinecntlr {
public string soID{get;set;}
List<Sales_Order_Line__c> saOrderLin;
public saleslinecntlr ()
{
System.debug('---------'+soID);
    saOrderLin=[Select id,name,Product__r.Name,Serial_Number__c,Quantity__c,Sale_Price__c,Total__c
                                             from Sales_Order_Line__c where salesorderid=:**soID** order by name];


}
    public List<Sales_Order_Line__c> getsaOrderLin() {
    return saOrderLin;
}

}
3

Component constructors are called before the property setters, this is not only the way things are with VF components but with Apex classes in general. The usual workaround to this is to apply your logic in the getsaOrderLin method instead, try this...

public List<Sales_Order_Line__c> getsaOrderLin() {
    if(saOrderLin==null)
       saOrderLin = [Select id,name,Product__r.Name,Serial_Number__c,Quantity__c,
                        Sale_Price__c,Total__c
                     from Sales_Order_Line__c 
                     where salesorderid=:soID order by name];
    return saOrderLin;
}
  • How did you decide whether we have to write our business logic inside getter of saOrderLin or soID? Is there any fixed rule? – doga Apr 15 '13 at 9:44
  • No fixed rule, the basic idea is the getters will be called after all the setters for the attributes. Be careful what you do though in the getters, as in VF the order in which these are called is not fixed. Its an anti-pattern workaround basically, set/get methods should never really have behaviour, if they do they should not effect the state of the object. The above cache style behaviour is borderline to this rule, so long as its used carefully. – Andrew Fawcett Apr 15 '13 at 10:17

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.