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
SabrentSabrent 

<apex:commandButton>

Can a command button call two actions at the same time?

 

I am in a situation where I have two methods Save and Update. I want to either invoke the two methods in one command button action or instantiate method1 in method2 so that the command button will first call method1 and then method2.

 

Thanks

Best Answer chosen by Admin (Salesforce Developers) 
LakshmanLakshman

Oh it seems you want to return to some page after all operations. Modify your code as:

 

public Pagereference uploadDocument(){

 

if (this.attachment != null) this.attachment = null;

 

this.isExempt = 'document';

 

this.attachment = newAttachment();

 

this.exemptionComments = '';

 

if (reqId != null){

 

Requirement__c reqDoc = [Select Id, Name, Template_Requirement__r.NameFromRequirement__cWhere Id = :this.reqId limit1];

 

if(reqDoc != null){

 

this.CurrentDocRequirement = reqDoc.Name;

 

this.CurrentDocReqTitle = reqDoc.Template_Requirement__r.Name;

}

}

this.RequirementID = this.reqId;


//Now save your record and return to the desired page you want

saveUpload();

 

PageReference newPage = new PageReference(newPageUrl);
newPage.setRedirect(true);
return newPage;

 

}

 

public void saveUpload() {

 

// logic here

}

 

Let me know if this works.

 

Regards,

Lakshman

All Answers

vagishvagish

Yes you can.

 

<apex:commandButton value="Click Me" action = "{!method2}" onClick = "name1"/>

<apex:actionFunction name = "name1" action = "{!method1}"/>

 

SabrentSabrent

cool thanks! I'll let you know how i go.

vagishvagish

A little correction in above post:

 

<apex:commandButton value="Click Me" action = "{!method2}" onClick = "name1();"/>

 

Call actionFunction in this way, and will work fine.

LakshmanLakshman

Rather you can also do like this:

 

<apex:commandButton value="Click Me" action = "{!method1}"/>

 

and then in the controller

 

public void method1()

{

//do desired operations and at the end call method2() as below

method2();

}

 

 

Regards,

Lakshman

SabrentSabrent

Thanks to vaghish and Lakshman;

 

I had earlier tried doing what Lakshman suggested. However got the error 'Non-void method might not return a value or might have statement after a return statement 

 

// This is my code 

 

publicPagereference uploadDocument(){

 

if (this.attachment != null) this.attachment = null;

 

this.isExempt = 'document';

 

this.attachment = newAttachment();

 

this.exemptionComments = '';

 

if (reqId != null){

 

Requirement__c reqDoc = [Select Id, Name, Template_Requirement__r.NameFromRequirement__cWhere Id = :this.reqId limit1];

 

if(reqDoc != null){

 

this.CurrentDocRequirement = reqDoc.Name;

 

this.CurrentDocReqTitle = reqDoc.Template_Requirement__r.Name;

}

}

this.RequirementID = this.reqId;

 

returnnull;

 

saveUpload();

 

}

 

public PageReference saveUpload() {

 

// logic here

}

LakshmanLakshman

Oh it seems you want to return to some page after all operations. Modify your code as:

 

public Pagereference uploadDocument(){

 

if (this.attachment != null) this.attachment = null;

 

this.isExempt = 'document';

 

this.attachment = newAttachment();

 

this.exemptionComments = '';

 

if (reqId != null){

 

Requirement__c reqDoc = [Select Id, Name, Template_Requirement__r.NameFromRequirement__cWhere Id = :this.reqId limit1];

 

if(reqDoc != null){

 

this.CurrentDocRequirement = reqDoc.Name;

 

this.CurrentDocReqTitle = reqDoc.Template_Requirement__r.Name;

}

}

this.RequirementID = this.reqId;


//Now save your record and return to the desired page you want

saveUpload();

 

PageReference newPage = new PageReference(newPageUrl);
newPage.setRedirect(true);
return newPage;

 

}

 

public void saveUpload() {

 

// logic here

}

 

Let me know if this works.

 

Regards,

Lakshman

This was selected as the best answer
SabrentSabrent

This worked!!! Cool!!! Thanks a lot. I really appreciate your help.