function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
rohitrrohitr 

Pass Value from Standard controller Constructor to global static method

My Controller Code:
global with sharing class CustomHighlightsPanelController {

    public Account accountInstance;
    global static String accountName {get; set;}
    global static String primeTabValue {get; set;}

    public CustomHighlightsPanelController(ApexPages.StandardController controller) {
        accountInstance = (Account)controller.getRecord();
        accountName = accountInstance.Id;
        System.debug('_______##############______'+accountName);
    }
    public PageReference FetchTabId() {
        return null;
    }
   
    @RemoteAction
    global static Premium_Payment__c[] loadPremiumPaidRecs() {
        List<Premium_Payment__c> premiumPaymentList = new List<Premium_Payment__c>();
        premiumPaymentList = [SELECT Id, Customer_Name__c, Paid_Premium__c, Policy__c, Premium_Paid_Date__c,Premium_Paid_Month_Year__c
                                FROM Premium_Payment__c WHERE Customer_Name__c =: accountName];
        return premiumPaymentList;
       
    }

}


accountName inside the SOQL query is getting null. but the variable accountName in the system.debug line gives a value.
How can i pass the contructor variable to the global static method which is defined as @RemoteAction.
Best Answer chosen by rohitr
bob_buzzardbob_buzzard
You should just be able to use merge syntax to add it to the parameter list for the remote action call:

function initCharts() {       
    CustomHighlightPanelController.loadPremiumPaidRecs('{!accountName}',
                              function(result, event){   
....... etc

You'll need to change the controller method to accept this parameter also:

global static Premium_Payment__c[] loadPremiumPaidRecs(String accName) {
   List<Premium_Payment__c> premiumPaymentList = new List<Premium_Payment__c>();
        premiumPaymentList = [SELECT Id, Customer_Name__c, Paid_Premium__c, Policy__c, Premium_Paid_Date__c,Premium_Paid_Month_Year__c
                                FROM Premium_Payment__c WHERE Customer_Name__c =: accName];
        return premiumPaymentList;
      
    }

You may also need to change the accountName to a regular public property in the class, rather than a global static, but it might work as it is.

All Answers

bob_buzzardbob_buzzard
The problem here is that the constructor won't be called when you execute a remote action method.  These are static methods so no instance of the controller is required.  Remote actions are stateless, so you need to pass all of the information required in the parameters.  

Therefore you'll need to alter your Visualforce page to include the accountName from the controller in the remote method call.  
rohitrrohitr
Thanks bob. But am not really good in concepts of javascript. So what changes i need to do in the code.
My javascript is defined like shown below:
<script type="text/javascript">
                 google.load('visualization', '1.0', {'packages':['corechart']});
                 google.setOnLoadCallback(initCharts);
          
                function initCharts() {        
                  CustomHighlightPanelController.loadPremiumPaidRecs(
                         function(result, event){
                                           var visualization = new google.visualization.PieChart(document.getElementById('PremiumPaidChart'));
                                           var data = new google.visualization.DataTable();
                                 data.addColumn('string', 'Month & Year');
                                 data.addColumn('number', 'Amount');
                                
                                 
                                           for(var i =0; i<result.length;i++){
                                    var r = result[i];
                                    data.addRow([r.Premium_Paid_Month_Year__c, r.Paid_Premium__c]);
                                  }
                                          visualization.draw(data, {legend : {position: 'none', textStyle: {color: 'black', fontSize: 10}}, width:window.innerWidth,vAxis:{title: 'Amount', textStyle:{fontSize: 10}},hAxis:{title: 'Month & Year', textStyle:{fontSize: 10},showTextEvery:1,slantedText:false}});
                          }, {escape:true});
                           }
            </script>

Please help me with the code.
Thanks in Advance.
bob_buzzardbob_buzzard
You should just be able to use merge syntax to add it to the parameter list for the remote action call:

function initCharts() {       
    CustomHighlightPanelController.loadPremiumPaidRecs('{!accountName}',
                              function(result, event){   
....... etc

You'll need to change the controller method to accept this parameter also:

global static Premium_Payment__c[] loadPremiumPaidRecs(String accName) {
   List<Premium_Payment__c> premiumPaymentList = new List<Premium_Payment__c>();
        premiumPaymentList = [SELECT Id, Customer_Name__c, Paid_Premium__c, Policy__c, Premium_Paid_Date__c,Premium_Paid_Month_Year__c
                                FROM Premium_Payment__c WHERE Customer_Name__c =: accName];
        return premiumPaymentList;
      
    }

You may also need to change the accountName to a regular public property in the class, rather than a global static, but it might work as it is.
This was selected as the best answer
rohitrrohitr
Thanks Bob.. You saved my day!!!
 
mitch_millermitch_miller
The example has been very helpful but I cannot get a variable passed into the static method based on the patterns above - suggestions, please:

<script  type="text/javascript">

   Visualforce.remoting.timeout = 120000; // Set timeout at page level

   var theLastName = document.getElementById('Participant__c.Last_Name__c').value;

   function getRemoteAccount() {

         // This remoting call will use the page's timeout value
         Visualforce.remoting.Manager.invokeAction(
             '{!$RemoteAction.Participant_Surgeries_controller.getSurgeries}',
             theLastName,
             handleResult
         );
     }
</script>

Hard coded last name returns the set but including variables get SOQL or other error:
@RemoteAction
global static List<Surgery__c> getSurgeries(){
  List<Surgery__c> surgeries = new List<Surgery__c>();
  
  try{
    surgeries = [Select  Name,  Surgery__c.Scheduled_Date__c  from Surgery__c
               Where Surgeon__r.Last_Name__c ='huang' ];    //theLastName or Last_Name__c is what I want to pass here
    } catch(DMLException e){
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, 'Error creating new page.'));
            return null;
    }
       return surgeries;
}

bob_buzzardbob_buzzard
You should post this as a separate topic - at the moment only those of us that participated in this question will see your issue, whereas everyone will see it if it is a new topic.
mitch_millermitch_miller
WIll do. I have come across more than a few of your posts (you rock) and I was hoping to get your attention.