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
AduttAdutt 

actionFunction not working

Hi All,
I have a custom VF page which performs the normal search functionality on Account and based on the selected account value wants to proceed further.
Problem:
I am not able to pass the selected account's id as action function is not working in the following code:

<apex:page controller="accountSearch">
<apex:form >
<script>
function toGetAccountId(id)
{
alert('test1');
variable1=document.getElementById(id).value;
xyz(variable1);
alert('test2');
}
</script>
<apex:pageBlock >

<apex:actionFunction name="xyz" action="{!samepage}">
<apex:param value="" assignTo="{!var1}"/>
</apex:actionFunction>


<apex:outputLabel value="Enter your search text:" >
<apex:inputText value="{!searchVar}">
</apex:inputText>
</apex:outputLabel>

<apex:commandLink value="Search" action="{!getAccounts}" />

<apex:pageBlockTable value="{!accList}" var="acc">
<apex:column headerValue="Account Name" > <apex:outputLink id="olId" onclick="toGetAccountId('{!$Component.olId}')">{!acc.name}
</apex:outputLink></apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
-----------------------------------------------------
controller:
public with sharing class accountSearch {
public String olId { get; set; }
public String searchVar { get; set; }
public String var;
public String var1{get;set;}
public String var2;
public list<Account> accList = new list<Account>();

public PageReference getAccounts() {
var='%'+searchVar+'%';
system.debug('aaaaaaaaaaaa'+var);
accList = [Select name,NumberOfEmployees from account where name LIKE:var ];
system.debug('vvvvv'+accList);
return null;
}

public list<Account> getAccList(){
return accList;
}
public pagereference samepage()
{
//PageReference curPage = ApexPages.currentPage();
system.debug('lllllllllll');
var2=var1;
system.debug('dddddddddddddd'+var2);
PageReference curPage = new Pagereference('/apex/testpage2');
curPage.setRedirect(true);
return curPage ;
}
}

After the list of accounts returned, if I select a particular account (onclick of a output link),the javascript is getting invoked and I am getting the alerts. But the action method (samepage()) is not invoked. (for testing purpose, I am just redirecting a test page on the action method and its not working)
Also, if I try saving <apex:actionFunction name="xyz" action="{samepage}"> instead of <apex:actionFunction name="xyz" action="{!samepage}"> , its getting saved which is wrong.

I am at lost. Please help..

Thanks.

Best Answer chosen by Admin (Salesforce Developers) 
izayizay

Have you tried using a commandLink instead of an outputLink? With a commandLink you can call the controller method and pass the id to the controller as well.

 

Hope this helps!

 

<apex:commandLink value="{!acc.Name}"  action="{!samePage}" reRender="elementIdToRerender">
   <apex:param value="{!acc.Id}" name="accountId" assignTo="{!var1}"/>
</apex:commandLink>

All Answers

izayizay

Have you tried using a commandLink instead of an outputLink? With a commandLink you can call the controller method and pass the id to the controller as well.

 

Hope this helps!

 

<apex:commandLink value="{!acc.Name}"  action="{!samePage}" reRender="elementIdToRerender">
   <apex:param value="{!acc.Id}" name="accountId" assignTo="{!var1}"/>
</apex:commandLink>
This was selected as the best answer
santoshgoresantoshgore

Hi,

 

I also came across this problem many times. Solution is use 'Name' attribute always while using apex:param tag.

 

e.g in ur case ,

 

<apex:actionFunction name="xyz" action="{!samepage}">
          <apex:param  name='var1'       'value=''  assignTo="{!var1}"/>
</apex:actionFunction>

 

AduttAdutt
Hi....
it ddn't worked :(
AduttAdutt
Hi izay,
Thanks for your solution...it works.