You need to sign in to do that
Don't have an account?

execute approval process from custom button - visualforce page
Hi,
I have a visualforce page with a controller extension. In my save method whenever the user clicks the save button and when the criteria is met the record has to be submitted for approval.
I would really appreciate if someone could please share some sample code for this requirement.
Thanks!
I have a visualforce page with a controller extension. In my save method whenever the user clicks the save button and when the criteria is met the record has to be submitted for approval.
I would really appreciate if someone could please share some sample code for this requirement.
Thanks!
You can choose to create your own dynamic approval process using apex and defined in the blog below:
http://shivasoft.in/blog/salesforce/dynamic-approval-process-based-on-the-apex-and-trigger/
Create an after insert trigger and pass the record id and a comment to the approvalInit method.
Ex.:
Trigger:
Trigger ApprovalInit on ObjectName (after insert){
for(ObjectName obj :Trigger.new){
ApprovalsHelper.approvalInit(obj.Id, 'This record has been submitted for your approval.');
}
}
Class:
public class ApprovalsHelper{
/**
** This method initiates an approval process for the given object
*/
public static void approvalInit(String id, String comment){
Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
req.setComments(comment);
req.setObjectId(obj.Id);
Approval.ProcessResult result;
try{
// submit the approval request for processing
result = Approval.process(req);
}catch(Exception e){
System.debug('No approval process has been setup yet.');
}
}
}
Replace the code in your ApprovalsHelper class with the following code... *Note that the approvalInit method now takes three parameter (objType, Id, comment) and it is an @future method so it will run a bit after it has been called. Refresh you browser to view the results after the record has been saved. The objType parameter is the object name as a string. If you are using a custom object don't forget to include the __c Ex.:
ApprovalsHelper.approvalInit('ObjectName', tobj.Id, 'This record has been submitted for your approval.');
@future
public static void approvalInit(String objType, String id, String comment){
sObject obj;
Schema.SObjectType targetType = Schema.getGlobalDescribe().get(objType);
if (targetType == null) {
// throw an exception
}else{
obj = Database.query(getQuery(targetType.newSObject(), null) + ' WHERE Id = \'' + id + '\'');
}
Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
req.setComments(comment);
req.setObjectId(obj.Id);
Approval.ProcessResult result;
try{
// submit the approval request for processing
result = Approval.process(req);
}catch(Exception e){
// display if the reqeust was successful
System.debug('No approval process has been setup yet.');
}
}
public static String getQuery(sObject obj, String mergedFields){
Map<String, Schema.SObjectField> fldObjMap = obj.getSObjectType().getDescribe().fields.getMap();
//The values from the map of fields
List<Schema.SObjectField> fldObjMapValues = fldObjMap.values();
String query = '';
String restricted = 'BillingLongitude,PersonOtherLongitude,ShippingLongitude,BillingLatitude,PersonOtherLatitude,'+
'ShippingLatitude,PersonMailingLatitude,PersonMailingLongitude,MailingLatitude,MailingLongitude,OtherLatitude,OtherLongitude,'+
'LastReferencedDate,';
//Add field names to string
for(Schema.SObjectField s : fldObjMapValues){
String fn = s.getDescribe().getName();
if(restricted.indexOf(fn) == -1){
query += fn+',';
}
}
System.debug('======================================= query: ' + query);
if(mergedFields != null){
query = 'SELECT ' + query + mergedFields + ' FROM ' + obj.getSObjectType().getDescribe().getName() + ' ';
}else{
query = 'SELECT ' + query.substring(0,query.lastIndexOf(',')) + ' FROM ' + obj.getSObjectType().getDescribe().getName() + ' ';
}
return query;
}