[SalesForce] getParameters().get(‘custompara’) Not Getting URL parameter

The below controller is attempting to grab an custom object id from the URL. The logs show that it fails to assign the string var para the id value from the URL. The system.debug for var para returns null. This controller must be structured wrong, please let me know where I have skipped a step.

public class getpara_Controller {

private ApexPages.StandardController sc;
public getpara_Controller(ApexPages.StandardController sc) {
    this.sc = sc;

}

public String para = ApexPages.currentPage().getParameters().get('custompara');

custom_object__c obj = new custom_object__c(lookup__c = para);

public PageReference insertRedirect() {

    sc.save();
    PageReference endpage = new PageReference('https://www.example.com');
   endpage.setRedirect(true);
    return endpage;
}

}

Best Answer

Those getParameters() line should be inside constructor as follows:

public class getpara_Controller {

    private ApexPages.StandardController sc;
    public String para {get;set;}

    public getpara_Controller(ApexPages.StandardController sc) {
        this.sc = sc;
        para = ApexPages.currentPage().getParameters().get('custompara');
        custom_object__c obj = new custom_object__c(lookup__c = para);
    }



    public PageReference insertRedirect() {

        sc.save();
        PageReference endpage = new PageReference('https://www.example.com');
        endpage.setRedirect(true);
        return endpage;
    }

}
Related Topic