Skip to main content
Sonali Mhatre 님이 #Visualforce에 질문했습니다
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?

 
답변 5개
  1. 2017년 4월 21일 오후 3:15
    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

     
0/9000