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
mt0859mt0859 

Calling an Apex function using Javascript on a VisualForce page

Hi All,

 

I'm trying to call an Apex function (in a controller extension class) using a Javascript "onclick" event and a VF ActionFunction. The scenario is this....

 

The button that triggers the "onclick" event is located in a VF page of form:

 

 

<apex:page controller="MyMasterPageController">

<apex:sectionHeader title="ATitle">
<apex:pageBlock >
<apex:composition template="MyChildPageComposition"/>

<apex:form >
<apex:commandButton value="Click Me" onclick="MyJavascriptFunc()"/>
</apex:form>

</apex:pageBlock>
</apex:sectionHeader>

</apex:page>

 

which is known as the "master" page.

 

 

The javascript function is located in the composition "MyChildPageComposition" of form:

 

 

<apex:page controller="MyMasterPageController" extensions="ControllerExtForChildPage">
<apex:form >

<apex:actionFunction action="{!myApexFunc}" name="MyJavascriptFunc"/>

<apex:pageBlock>
<apex:pageBlockSection title="ASectionTitle">

<!-- Input fields/bulk of VF page etc goes here -->

</apex:pageBlockSection>
</apex:pageBlock>

</apex:form>
</apex:page>

 

which is known as the "child" page and displayed within the "master" page using the <apex:composition> functionality.

 

 

The Apex function "myApexFunc" is found in the class "ControllerExtForChildPageComposition" and is of form:

 

 

public PageReference myApexFunc() {

// do something here
system.debug('Print some debug here');

return null;
}

 

 

 

Now, my issue is that if I create the VF pages for real as basic as those examples above, "myApexFunc" is successfully called (the debug is printed, and any other code in it runs) when the "Click Me" button in the master page is clicked.

 

However, when I have attempted to incorprate the basic apex and VF code above into existing master/child pages and their associated controllers and extensions, "myApexFunc" is never successfully called. In fact, if the child page is subsituted for a child page of this form:

 

 

 

<apex:page controller="MyMasterPageController" extensions="ControllerExtForChildPage">
<apex:form >

<apex:actionFunction action="{!myApexFunc}" name="AnotherJavascriptFunc"/>

<script>

function MyJavascriptFunc() {
alert('Running Javascript');
AnotherJavascriptFunc();
alert('Finishing javascript');
}

</script>

<apex:pageBlock>
<apex:pageBlockSection title="ASectionTitle">

<!-- Input fields/bulk of VF page etc goes here -->

</apex:pageBlockSection>
</apex:pageBlock>

</apex:form>
</apex:page>

 

which is incorprated into an existing child page,both alerts are run (proving that the link between the "onclick" event in the master page and "MyJavascriptFunc" in the child page composition is there, but "myApexFunc" is still not called! Can anyone shed any light on this? Is there any VF or Apex code which is incompatible with using Javascript and action functions? As I said before, when it's in its basic forms above, everything works, just not when other VF and apex code are added.

 

Many thanks,

 

Marco

 

 

 

 

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Rajesh ShahRajesh Shah

I think it is a bug. Even I didn't had to rerender anything on the page but still had to include it. I rerender the whole page i.e. gave the page component an Id and used that in actionFunction for rerendering.

 

Let me know if this works for you. If yes, then this may be a bug we can report to Salesforce.

All Answers

Rajesh ShahRajesh Shah
I once had a similar problem. Try adding rerender attribute to the actionFunction component. It worked for me.
mt0859mt0859

Hi Rajesh,

 

Thanks for the reply. Is there any specific part of either/both pages I should rerender? Or is it just a SF bug whereby not having a rerender attribute in an action function makes it keel over (silently)? Just to clarify, there's no issue with displaying anything on either VF page, merely that the apex function doesn't execute.

 

Regards,

 

Marco

Rajesh ShahRajesh Shah

I think it is a bug. Even I didn't had to rerender anything on the page but still had to include it. I rerender the whole page i.e. gave the page component an Id and used that in actionFunction for rerendering.

 

Let me know if this works for you. If yes, then this may be a bug we can report to Salesforce.

This was selected as the best answer
mt0859mt0859

Rajesh,

 

Many thanks - your fix works (assigning an ID to the page itself, and assigning that to the rerender in the action function), which would suggest that it is a SF bug.

 

There was also a parallel issue with my VF page whereby there were input fields on the child page used to populate  say a Contact record. It seems that although no insert/upsert of the contact record was supposed to occur on the button click (it was *not* in the "myApexFunc" function), SF was refreshing the master page and composition child page anyway after the button click (and the alerts appeared) and, for some unknown reason, attempting to insert/upsert the contact record (which had not had its mandatory fields populated) despite not being asked to. Instead of producing an error as normal saying mandatory field were missing, it failed silently.

 

It would appear the mystery is solved!

 

Regards,

 

Marco

mtbclimbermtbclimber
Adding a rerender target should work around the issue which has been fixed in the forthcoming summer '09 release.  From that point on rerender is not required on actionFunction and when not provided the full page will refresh.
SuvraSuvra

Hi Rajesh,

Need your help in asimilar kibd of problem.

 

I need to implement the auto completion feature in my page. For that I've a text box. WHenever some text is keyed in, I need to call a Controller class for "onkeyevent". Som I followed the same approach whatever you specified, from the script, alert is executing and giving correct value, but Controller class is not getting executed.

 

Here is my sample code :

<apex:form >
    <apex:actionFunction action="{!ShowTextValueInternet}" name="MyJavascriptFunc" rendered="internetPagination"/>
    <script>               
        function grabInputName() {            
        alert('Running Javascript');           
        MyJavascriptFunc();           
        alert('Finishing javascript');      
     }
     </script>
       

  <apex:outputpanel id="internetPagination" rendered="{!visionListingtotal>0}">
            <apex:pageblock title="Internet Listings" >
                <apex:outputpanel layout="block" styleclass="centerSpace">&nbsp;&nbsp;&nbsp;&nbsp;
                    <apex:inputText id="autoCompleteTextbyName" onkeydown="grabInputName()">
                      </apex:inputText>

  // other stuffs

</apex:outputpanel>

 

Here When calling this page, the text field displays. when i'm entring some value into text field, it is popping up the alert "Running Javascript". But after that the code is not moving at all. Please help..

 

Thanks in advance,

Rajesh ShahRajesh Shah
You have to use rerender attribute in the actionFunction. In the code, you have used rendered. Change that and I think it will start working.
Message Edited by Rajesh Shah on 05-26-2009 04:04 PM
SuvraSuvra
Thanks a lot Rajesh for your help.
mallikammallikam

Rajesh,

 

I am having problems with actionFunction and I am wondering if you can help..my VF page has some javascript fn which is calling controller method but the controller method is never being called..

 

   <apex:form >
                 <apex:actionFunction name="populateFields" action="{!populateFields}" rerender="Email" status="Estatus"/>
                                 
         <apex:pageBlock mode="edit" id="thePageBlock">
          
         <apex:actionRegion >
         <apex:pageBlockSection columns="1">                                     
             <apex:inputField value="{!custObj.Employee_Name__c}" onselect="populateFields(this)" id="name"/>    
         </apex:pageBlockSection>
         <apex:pageBlockSection >                     
             <apex:outputPanel id="email">
             <apex:outputLabel value="Employee Email" for="em" />
             <apex:panelGrid columns="2">
                   <apex:inputField value="{!custObj.Employee_Email__c}" rendered="false"/>
                   <apex:inputText value="{!contact.Email}" id="em" title="Email"/>
                   <apex:actionSupport event="onclick" reRender="email" status="Estatus"/>

 

.....

 

The actionSupport event is working fine...but I want to generate the Email when user selects a name and hence I wrote javascript event for onselect..(onselect is not working with actionsupport..I think its a SF bug) and then I am using actionFunction to call populateFields and then reredering the Email...basically I want the email to be autopopulated as soon as a name is submitted either through onselect or onclick...but like I said populateFields in controller is never being called..

Rajesh ShahRajesh Shah

Hi,

 

Sorry for the late reply. Were you able to solve the problem. 

 

I m not sure why the problem is coming. However, you can try the following thing to debug:

 

Change the event to onChange or something like and see if the function is getting called. Might be that onselect is not supported by inputField.

 

 

 

mallikammallikam
Hi Rajesh, thanks for the reply. The problem is solved for now..I am calling a Javascript function on onselect, which is further calling Apex function through actionFunction.