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
Victor19Victor19 

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!
Sonam_SFDCSonam_SFDC
Hi Victor,

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/
izay.ramos-irizarry1.3893936724146558E12izay.ramos-irizarry1.3893936724146558E12
Create the following class... 
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.');
        }
    }
}
izay.ramos-irizarry1.3893936724146558E12izay.ramos-irizarry1.3893936724146558E12
Victor,

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;
    }