0

what is the best practice to call two controller method from a single commandbutton click. I have a requirement that on single buttonclick, page will navigate to first page and also call add new method. currently commandbutton calls only one controller method AddMore

<apex:commandButton value="Add New" action="{!AddMore}" onclick="this.value = 'Adding...'" id="AddMoreButton"    /> 

Method 1 -

public void AddMore()
{
        //count = count+1;
        Abv_Core_KAM_Competitor_Sales__c  newRec=new Abv_Core_KAM_Competitor_Sales__c  (Abv_Core_KAM_Account__c =Acc_Id);
        if(records.size()>0)
        {
            records.add(0,newRec);}
        else
        {    
           records.add(newRec);}
    }

Method 2 -

    public boolean getprev()
    {
        if(index == 0)
        return true;
        else
        return false;
    }  

that also need to be called from same commandbutton

1
  • 2
    why not call your getPrev() method in your AddMore() method itself?
    – AslamK
    Jan 7, 2015 at 11:17

3 Answers 3

3

The simplest and legitimate way to do this is by calling getprev method at the end of AddMore

Something like this:

    public void AddMore()
{
    //count = count+1;
    Abv_Core_KAM_Competitor_Sales__c  newRec=new Abv_Core_KAM_Competitor_Sales__c  (Abv_Core_KAM_Account__c =Acc_Id);
    if(records.size()>0)
    {
        records.add(0,newRec);}
    else
    {    
       records.add(newRec);}
    }
    getprev(); // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
}
1

Following two lines of code should definitely work in your case:

<apex:actionFunction action="{!getprev}" name="methodInJavascript" >
<apex:commandButton value="Add New" action="{!AddMore}" onclick="methodInJavascript();"/>
2
  • tried the same but its not working
    – user59759
    Jan 7, 2015 at 11:40
  • @user59759 it works for me .... can you please check if you are missing something. Do you get any specific errors?
    – Yogesh D
    Jan 7, 2015 at 11:43
0

Try below,

<apex:actionFunction name="executeClass1Method" action="{!Class1Method}"/>
<apex:commandButton onClick="executeClass1Method()" action="{!Class2Method}">
</apex:commandButton >

Dont forget to accept the answer if it works

2
  • I've tried this:<apex:actionFunction name="n" action="{!beginning}"/> <apex:commandButton value="Add New" onclick="n()" id="AddMoreButton" action="{!AddMore}" /> but it only adds new row but doesnt navigates to 1st page
    – user59759
    Jan 7, 2015 at 11:18
  • @user59759, How do you expect navigating somewhere if you don't have any navigation method/function? Sep 19, 2016 at 13:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .