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
Maf_007Maf_007 

Action Function not Invoking the referenced method

Hi All,

I am trying to do a really simple thing here but it's not working. I am trying to display a page message from action function method but it's not working. I have checked the debug log as well but it seems like it's not invoking the method at all. Could anyone see if I am doing anything wrong here:

<apex:page standardController="MyObject__c" extensions="MyControllerExt" >
    <apex:form id="MainForm">
    
        <apex:actionFunction name="InvokeActionFunction" action="{!InvokeActionFunction}" rendered="{!displayactionfunction}" reRender="MainForm,Msg"/>
        
        <apex:pageBlock>
            <apex:pageBlockSection>
                    <apex:messages id="Msg"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
        
    </apex:form>
</apex:page>

Controller Ext:

public class MyControllerExt{
    Public static Customer__c Cus {get; set;}
    public boolean displayactionfunction{get; set;}
    public MyControllerExt(ApexPages.StandardController CusController)
    {
		System.debug('Record to process is: ------------->'+CusController.getRecord());
        Cus = (Customer__c)CusController.getRecord();
        displayactionfunction = true;
    }
    
    Public void InvokeActionFunction()
    {
        ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL,System.Label.Problem_Saving_Record);
        ApexPages.addMessage(myMsg);
        System.debug('Page Message is-------------------------------->'+myMsg);
        displayactionfunction = false;
    }
}

Thanks in advance!
Best Answer chosen by Maf_007
logontokartiklogontokartik
Well you are not calling the actionFunction and hence its not getting invoked. Maybe try something like below on your page. 
<apex:page standardController="MyObject__c" extensions="MyControllerExt" >
    <script>
         window.onload=function(){
               InvokeActionFunction();
          };
    <script>
    <apex:form id="MainForm">
    
        <apex:actionFunction name="InvokeActionFunction" action="{!InvokeActionFunction}" rendered="{!displayactionfunction}" reRender="MainForm,Msg"/>
        
        <apex:pageBlock>
            <apex:pageBlockSection>
                    <apex:messages id="Msg"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
        
    </apex:form>
</apex:page>
ActionFunction behind the scenes is nothing but a Javascript function that calls one of the methods in our controller. (similar to JS Remoting) but only controller methods. 

Hope this helps

All Answers

logontokartiklogontokartik
Well you are not calling the actionFunction and hence its not getting invoked. Maybe try something like below on your page. 
<apex:page standardController="MyObject__c" extensions="MyControllerExt" >
    <script>
         window.onload=function(){
               InvokeActionFunction();
          };
    <script>
    <apex:form id="MainForm">
    
        <apex:actionFunction name="InvokeActionFunction" action="{!InvokeActionFunction}" rendered="{!displayactionfunction}" reRender="MainForm,Msg"/>
        
        <apex:pageBlock>
            <apex:pageBlockSection>
                    <apex:messages id="Msg"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
        
    </apex:form>
</apex:page>
ActionFunction behind the scenes is nothing but a Javascript function that calls one of the methods in our controller. (similar to JS Remoting) but only controller methods. 

Hope this helps

This was selected as the best answer
Maf_007Maf_007
Hi Thanks for the clarification. Just a quick one however, any idea why my page message is not displaying on rerender?
Maf_007Maf_007
Please ignore it's working actually. thanks again
logontokartiklogontokartik
Have to tried using
<apex:pageMessages id="theMessages"/> tag to display the messages on Visualforce page
Ravi NarayananRavi Narayanan
<apex:pageMessages id="msg"/>  try this