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
Anurag Singh 17Anurag Singh 17 

Action function not working

Hi guys have a
page :.
<apex:commandButton value="First" onclick="getscore('First')" />
</apex:pageBlock>
<apex:actionFunction action="{!score}" name="getscore"  reRender="content">
 <apex:param name="myParam" value=""/>
</apex:actionFunction>

Extension  method:

public void score() {
        string passedParam1 = Apexpages.currentPage().getParameters().get('myParam');
        system.debug('ssssssssssssss'+passedParam1);
    }

The method is not being called at all any help here would be appreciated
Best Answer chosen by Anurag Singh 17
Jigar.LakhaniJigar.Lakhani
Hello Anurag,

It looks like your code is correct, you are calling actionfunction directly from button onclick with parameter.
But may be arrangement of code is not proper, can you please try to define <apex:actionFunction> above <apex:commandButton> ?

I am not sure but you should try like below.
 
<apex:actionFunction action="{!score}" name="getscore" reRender="content">
     <apex:param name="myParam" value="" />
</apex:actionFunction>
<apex:pageBlock>
    <apex:commandButton value="First" onclick="getscore('First');return false;" />
</apex:pageBlock>

Please let me know if it works.

Thanks and Cheers,
Jigar

 

All Answers

Jigar.LakhaniJigar.Lakhani
Hello Anurag,

It looks like your code is correct, you are calling actionfunction directly from button onclick with parameter.
But may be arrangement of code is not proper, can you please try to define <apex:actionFunction> above <apex:commandButton> ?

I am not sure but you should try like below.
 
<apex:actionFunction action="{!score}" name="getscore" reRender="content">
     <apex:param name="myParam" value="" />
</apex:actionFunction>
<apex:pageBlock>
    <apex:commandButton value="First" onclick="getscore('First');return false;" />
</apex:pageBlock>

Please let me know if it works.

Thanks and Cheers,
Jigar

 
This was selected as the best answer
Anurag Singh 17Anurag Singh 17
@Mr Salesforce Its pretty wierd any explanation why the apex function on top made it work?
Jigar.LakhaniJigar.Lakhani
@Anurag
Internally the name of actionfunction is working as javascript function name.
Since if we do not define that function before using of it, it will not find that actionfunction name.
Anurag Singh 17Anurag Singh 17
@ Mr Salesforce,
I understood that but if i change the command button to outputlabel and use the same code with action function below the output label tags it works fine! so I am a bit confused
Jigar.LakhaniJigar.Lakhani

@Anurag
Actually the issue is not common which is not replicated every time in every visualforce page, may be your other code effecting it.
If you try to other simple visualforce page and try to replicate the same issue, may be you'll not find it.

But according to best practice we should arrange visualforce page code like below.

<apex:page>
<apex:form>
<style></style>
<script><script>
<apex:actionFunction>
<apex:pageMessages>
<apex:pageBlock>
....

Andrew B. DavisAndrew B. Davis
If you want to use an ActionFunction to call the controller action, you should probably use:
​<input type="button" value="First" onclick="getscore('First')" />

If you use <apex:commandButton onclick="getscore('First')" /> what happens is that you have a "race condition" where the commandButton's built-in functionality will try to fire at the same time as the functionality defined in your onclick method. Another option would be to set your onclick="getscore('First'); return false;" which should prevent the commandButton action from firing.

See here: http://salesforce.stackexchange.com/questions/29400/calling-apexactionfunction-on-apexcommandbuttons-onclick-event
Sangeeta Yadav 29Sangeeta Yadav 29
Hi, 
I am facing same issue. i put my action function before outlink but it doesn't work. can you please help me.
<apex:actionFunction  action="{!getDetails}" name="accDetails" rerender="display">
                <apex:param name="accId" value="" assignTo="{!accid}"/>
  </apex:actionFunction>
<apex:outputLink onclick="accDetails('{!a.id}');return false;">
                            {!a.Name}                             
</apex:outputLink>
here i am making table for Accounts and a.id is id of an account which i want to pass to my parameter accid in controller. please help me out.
Thank you