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
Sonali Mhatre 1Sonali Mhatre 1 

Unable to call Controller Extension's method from VisualForce page

I want to call a method from controller extension on the onfocus event from Visualforce Code.My Controller Extension method has a System.debug statement. The following debug method call is not displayed in the Developer Console.

Visualforce Code where call to Controller Extension's method is made:
        <apex:inputField value="{!SomeLookupField}"  onchange="getMethod()"/>

Controller Extension Code:
             public PageReference getMethod() 
               {
                       System.debug('inside method getMethod()');
                }
 
I tried to refer to several posts on the developer forum but it does not seem to work still.
Can you please suggest on this?







 
v varaprasadv varaprasad
Hi Sonali 

<apex:inputField value="{!SomeLookupField}"  onchange="getMethod()"/> 
in above tag onchange is a javascript event.
So directly we cant call controller methods from javascript.
so instead of that we will use actionsupport or action function.

plz check sample code below
<apex:page controller="exampleCon">
	<apex:form>
		<apex:outputpanel id="counter">
			<apex:outputText value="Click Me!: {!count}"/>
				<apex:actionSupport event="onclick"
				action="{!incrementCounter}"
			rerender="counter" status="counterStatus"/>
		</apex:outputpanel>
		
		<apex:actionStatus id="counterStatus"
			startText=" (incrementing...)"
			stopText=" (done)"/>
	</apex:form>
</apex:page>


/*** Controller: ***/
public class exampleCon {
	Integer count = 0;
	public PageReference incrementCounter() {
		count++;
		return null;
	}
	public Integer getCount() {
		return count;
	}
}
 
<apex:page standardController="Contact" extensions="ActionSupport">
     <apex:form >
         <apex:pageBlock id="Change">
             <apex:pageBlockSection >
                 <apex:inputField value="{!Contact.lastname}"/>
                 
                 
                 <apex:inputField value="{!Contact.AccountID}">
                      <apex:actionSupport action="{!Testit}" event="onchange" reRender="Change"/>
                 </apex:inputField>
                 
                 
                 <apex:inputField value="{!Contact.Phone}"/>
                 
             </apex:pageBlockSection>
         </apex:pageBlock>
     </apex:form>
</apex:page>


public class ActionSupport {

    public Contact ConRec;
    public ActionSupport(ApexPages.StandardController controller) {
           ConRec=(Contact)controller.getRecord();
           //ConRec.phone='99999999999';
    }
    public void testit()
    {
        system.debug('Account ID===========>'+ConRec.accountid);        
        Account varacc=[select id,name,phone from Account where id=:ConRec.accountid];
        ConRec.phone=varacc.phone;
    }

}


please let me know if you have any questions

Thanks
Varaprasad

 
Sonali Mhatre 1Sonali Mhatre 1
Hi Varaprasad,

Thanks for your suggestion.
I tried the way you told. When I select the value from lookup field on Visualforce Page, the log generates on Developer Console but it doesn't show the debug statement. Even after adding a filter it does not display the debug statement.

Visualforce Code where call to Controller Extension's method is made:
          <apex:inputField value="{!SomeLookupField}">
          <apex:actionSupport action="{!getMethod}" event="onchange" reRender="DsptDtls"/>
           </apex:inputField>

Controller Extension Code:
             public PageReference getMethod() 
               {
                       System.debug('inside method getMethod()');
                }

Can you please help me on this?
v varaprasadv varaprasad
Hi Sonali

please check once your debuglog level settings.

User-added image

In above examples check second one it is working.


thanks
varaprasad
Sonali Mhatre 1Sonali Mhatre 1
Hi Varaprasad,

As per your suggession I checked my debug settings and they are as per you specified but still I am stuck on the same problem.
Can you please take a look into my code? Let me know if any changes required in code.

Visualforce Code where call to Controller Extension's method is made:
          <apex:inputField value="{!SomeLookupField}">
          <apex:actionSupport action="{!GetMethod}" event="onchange" reRender="DsptDtls"/>
           </apex:inputField>

Controller Extension Code:
             public PageReference getMethod() 
               {
                       System.debug('inside method getMethod()');
                }


Thanks,
Sonali.
sunny522sunny522
Hi Sonali,
   check the example below.
http://salesforceglobe4u.blogspot.com/2017/05/actionfunction-in-salesforce.html