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
mitch_millermitch_miller 

Pass Value from Standard controller to global static methed

mitch_miller
This pattern for the controller seems to work for others but I am unable to access any variables in the controller to pass thequery as an arguement - I assume because it is static:

global with sharing class Participant_Surgeries_controller {


public String [] participants{get;set;}
private final Participant__c participant;

global Static String lastName;

  ApexPages.StandardController m_controller;

    public Participant_Surgeries_controller(ApexPages.StandardController controller) {
    m_controller = controller;
    this.participant = (Participant__c) controller.getRecord();
}
@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 = :this.participant.Last_Name__c ]; //variations with 'this.participant' or local variable are not in scope 
                                                                                                                                                  // but a hard coded value works fine.

    } catch(DMLException e){
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, 'Error creating new page.'));
            return null;
    }
       return surgeries;
}
}
bob_buzzardbob_buzzard
You won't be able to access any properties from the controller as there is no instance of the controller for static methods - hence they are known as 'stateless' as they have no notion of the current controller state.  

You'll need to pass the participant last name as a parameter to the remote method from the page if you want to access it..  
mitch_millermitch_miller
Thanks for the response. When taking your direction and passing in the string from the page I am getting a SOQL error that I do not see if simply replace the variable with a string. Error = Content cannot be displayed: Unknown property 'Participant__cStandardController.surgeries'

global with sharing class Participant_Surgeries_controller {


public String [] participants{get;set;}
private final Participant__c participant;

global Static String staticLastName;


  ApexPages.StandardController m_controller;

    public Participant_Surgeries_controller(ApexPages.StandardController controller) {
    m_controller = controller;
    this.participant = (Participant__c) controller.getRecord();

}


   
@RemoteAction
global static List<Surgery__c> getSurgeries(String lastName){
  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 = :lastName]
; //query works in Developer Console & method works when string 'smith' used instead of :lastName

    } catch(DMLException e){
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, 'Error creating new page.'));
            return null;
    }
       return surgeries;
}
}

<apex:page standardController="Participant__c" extensions="Participant_Surgeries_controller" sidebar="false" showHeader="false"> 
<Head>
</Head>
<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
         );
     }
   
    //dummy function
    function handleResult(result, event){}
   
</script>
<apex:form >
    <apex:pageBlock >

      <apex:repeat value="{!surgeries}" var="surgery">

        <apex:outputPanel >
         <apex:outputLink value="{!surgery.id}" >{!surgery.name} - {!surgery.Scheduled_Date__c}</apex:outputLink>
         <br/>
        </apex:outputPanel>

      </apex:repeat>

    </apex:pageBlock>



</apex:form>
mitch_millermitch_miller
I added a formula field that is updated with the surgeon last name so that I do not even reference the Participant__c object and I still get teh same error: 
Content cannot be displayed: Unknown property 'Participant__cStandardController.surgeries'

@RemoteAction
global static List<Surgery__c> getSurgeries(String lastName){
  List<Surgery__c> surgeries = new List<Surgery__c>();
   
  try{
    surgeries = [Select  Name,  Surgery__c.Scheduled_Date__c from Surgery__c
               Where Surgeon_Last_Name_2__c = :lastName];
    } catch(DMLException e){
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, 'Error creating new page.'));
            return null;
    }
       return surgeries;
}
bob_buzzardbob_buzzard
Pretty much the same issue I'm afraid.  You have declared your getSurgeries method as a global static remoteaction, so you can only use this via Visualforce remoting.  In your page you are trying to iterate the results of this method call via standard components:

<apex:repeat value="{!surgeries}" var="surgery">

        <apex:outputPanel >
         <apex:outputLink value="{!surgery.id}" >{!surgery.name} - {!surgery.Scheduled_Date__c}</apex:outputLink>
         <br/>
        </apex:outputPanel>

      </apex:repeat>

but there is no getSurgeries method that can be used in this way.  When you use remoting, its up to you to parse the results and manipulate the DOM.
 
mitch_millermitch_miller
Aprreciate the feed-back and I am pretty sure that some of what you are saying is going over my head, but I am using the pattern pointed out in this exsample:

http://www.salesforce.com/us/developer/docs/pages/Content/pages_js_remoting_example.htm

Also, the VisualForce snippet that you posted works fine when I replace the :lastName variable with a hard coded string containing an actual last name, e.g. 'smith'. 

I am not sure why the error complains about Participant, which is a child object of Surgery, when a string is not used, but I suspect that my problem might be there:

Content cannot be displayed: Unknown property 'Participant__cStandardController.surgeries'



bob_buzzardbob_buzzard
The error is because your page uses a standard controller of participant__c - as the page rendering engine can't find a "surgeries" getter, it assumes the problem is with the standard controller (hence Participant__cStandardController).

I'm afraid that you can't use standard components on methods that are annotated as @RemoteAction.
mitch_millermitch_miller
It's finally clear to me. Appreciate all the effort you took explaining everything.