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
Pradeep Musthi 9Pradeep Musthi 9 

<apex:actionfunction> not Working Properly

VisualForcePage 
<apex:page controller="ActionController" >
   <script type="text/javascript">
    function validate()
    {
        ClickMe();
        }
    </script>
    <apex:form id="f1">
    <apex:actionFunction action="{!Click}" name="ClickMe" rerender="pg"/>
    <apex:pageBlock id="pg">
        <apex:outputLabel value="Enter Name">
        <apex:inputText value="{!Name}" id="n1"/>
        </apex:outputLabel>
        
         <apex:commandButton value="Refresh" onclick="validate();" />
        </apex:pageBlock>
    </apex:form>
    <apex:pageBlock>
    <apex:outputText value="{!Name1}"></apex:outputText>
    </apex:pageBlock>
</apex:page>
Controller
public class ActionController {
    public String Name{Get;Set;}
    public String Name1{Get;Set;}
public PageReference Click()
{

    Name1=Name; 
    system.debug(Name1);
    return null;
}
}

The function is like, it takes input from the page and displays the same value in the output text in a pageblock,but my action function is not working
is der any fault in my code??
 
Ajay K DubediAjay K Dubedi
Hi Pradeep,

You can try the following Sample code :

VisualForcePage :
<apex:page controller="ActionController" id="myPage">

    <script type="text/javascript">
        function DisableButton() {
            var btn = document.getElementById("{!$Component.myPage.myForm.myPageBlock.myPageBlockSection.myPageBlockSectionItem.btnSearch}");
            btn.disabled = true;
        }
        function EnableButton() {
            var btn = document.getElementById("{!$Component.myPage.myForm.myPageBlock.myPageBlockSection.myPageBlockSectionItem.btnSearch}");
            btn.disabled = false;
        }
    </script>
    <apex:form id="myForm">
        <apex:pageBlock mode="edit" id="myPageBlock">
            <apex:pageBlockSection id="myPageBlockSection">
                <apex:pageBlockSectionItem id="myPageBlockSectionItem">
                    <apex:outputLabel for="searchText">Search Text</apex:outputLabel>
                    <apex:panelGroup id="myPanelGroup">
                        <apex:inputText id="searchText" value="{!searchText}"/>
                        <apex:commandButton status="AjaxStatus" action="{!doSearch}" id="btnSearch" rerender="results" value="Search"/>
                        <apex:actionStatus id="AjaxStatus" onstart="DisableButton()" onstop="EnableButton()" ></apex:actionStatus>
                    </apex:panelGroup>
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
            <apex:pageBlockSection title="Results" id="results" columns="1">
                <apex:pageBlockTable value="{!results}" var="item" rendered="{!HasResults}">
                    <apex:column value="{!item.name}"/>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller :
public class ActionController {
    public String searchText { get; set; }
    public List<Contact> results { get; set; }

    public void DoSearch() {
        results = (List<Contact>)[FIND :searchText RETURNING Contact(Name, Email, Phone)][0];
    }

    public boolean getHasResults() {
        return results != null && results.size() > 0;
    }
}

Please mark as best answer if it helps you.

Thank you, 
Ajay Dubedi
Pradeep Musthi 9Pradeep Musthi 9
Hi Ajay,
can u tell me what is the problem in my code
Anil GiriAnil Giri
Hi Pradeep,


I did changes in VF page and Controller please look into it

didnt required to use second veriable Name1.

I run following code in my Org. its working fine.
public class ActionController {
    public String Name{Get;Set;}
    public String Name1{Get;Set;}
    public void Click(){
         //Name1=Name; 
         system.debug(Name1);
        // return null;
    }
}

VF Page:
No need to used sript. you can directly to actionfunction and button.
<apex:page controller="ActionController" >
   /*<script type="text/javascript">
    function validate(){
        ClickMe();
    }
   </script>*/
    <apex:form id="f1">
        <apex:actionFunction action="{!Click}" name="ClickMe" rerender="pg"/>
        <apex:pageBlock >
            <apex:outputLabel value="Enter Name">
                <apex:inputText value="{!Name}" id="n1"/>
            </apex:outputLabel>
            <apex:commandButton value="Refresh" onclick="ClickMe();" />
        </apex:pageBlock>
        
        <apex:pageBlock  id="pg">
            <apex:outputText value="{!Name}"></apex:outputText>
        </apex:pageBlock>
    </apex:form>
    
</apex:page>

this  will help for you.

Regards,
Anil 
Bhargavi TunuguntlaBhargavi Tunuguntla
Hi Pradeep,

Try the below code: (this will work fine)
 
<apex:page controller="ActionController" >
   <script type="text/javascript">
    function validate()
    {
        ClickMe();
        }
    </script>
    <apex:form id="f1">
    <apex:actionFunction action="{!Click}" name="ClickMe" rerender="pg1"/>
    <apex:pageBlock id="pg">
        <apex:outputLabel value="Enter Name">
        <apex:inputText value="{!Name}" id="n1"/>
        </apex:outputLabel>
        
         <apex:commandButton value="Refresh" onclick="validate();return false;"/>
        </apex:pageBlock>
   
    <apex:pageBlock id="pg1">
    <apex:outputText value="{!Name1}"></apex:outputText>
    </apex:pageBlock>
         </apex:form>
</apex:page>

Due to call to js function ,the whole vf page is getting refreshed so if we return false in the onclick function it will rerender as required.

Thanks.