+ Start a Discussion
Soundar Raj PonpandiSoundar Raj Ponpandi 

How to skip approval steps if submitted by sales Rep ?


I have created a standard approval process and apex class to trigger this approval process. Here i am facing one challanges, while submitting the approval process it should skip the step 1 (if submitter is sales rep). Please find the approver hierarchy below.

1. Sales Coordinator
2. Slaes Rep
3. Sales Manager
4. Division Manager

If sales rep is submit for an approval it should skip first step (Sales coordinator) approval. 

here i have written apex class, Please check and let me know where i did mistake.
public class GD_QuoteApprovalController {
    public static GD_Quote__c getQuoteDetails(String quoteId){
            GD_Quote__c quote = new GD_Quote__c();
            List<User> approvalUsers = new List<User>();
            quote = [SELECT Id,GD_Status__c,OwnerId,GD_Quote_Margin__c,GD_Warranty_Years__c,GD_Has_FOC__c,
                     FROM GD_Quote__c WHERE Id=: quoteId ];
            approvalUsers = [SELECT Id,Name FROM User WHERE userRole.Name =: Label.GD_ME_Division_Manager_Role_Name];
            User loggedInUser = [SELECT Id,Name,profile.name FROM User WHERE Id=:userInfo.getUserId()];
            User createdBy = [SELECT Id,Name,profile.name FROM User WHERE Id=: quote.CreatedById];
            string coordinator = loggedInUser.profile.name;
            string createdByProfile = createdBy.profile.name;
                throw new AuraHandledException('No Division manager assigned.');    
            quote.GD_Sales_Manager__c = quote.GD_Sales_Rep__r.ManagerId;
            quote.GD_Division_Manager__c = approvalUsers.get(0).Id;
            //Submitted By Sales Coordinator
                quote.GD_Sales_Coordinator__c = loggedInUser.Id;
            }else if(createdByProfile.contains('Coordinator')){
                quote.GD_Sales_Coordinator__c = quote.CreatedById;
            //Submitted By Sales Rep
            if(coordinator.contains('Sales Rep')){
                quote.GD_Sales_Coordinator__c = loggedInUser.Id;
                quote.GD_Submitted_By_Sales_Rep__c = true;
            }else if(createdByProfile.contains('Sales Rep')){
                quote.GD_Sales_Coordinator__c = quote.CreatedById;
                quote.GD_Submitted_By_Sales_Rep__c = true;

            update quote;
            return quote;
        }catch(Exception e){
            throw new AuraHandledException(e.getMessage());    
    public static void submitForApproval(GD_Quote__c quoteRecord,String approvalComments){
        STRING GD_ME_GI_ET = System.Label.ME_Business_Unit_GI_ET;
        STRING GD_ME_CRITICAL_CARE = System.Label.ME_Business_Unit_Critical_Care;
        STRING GD_ME_INFECTION_CONTROL = System.Label.ME_Business_Unit_Infection_Control;
        STRING GD_ME_CWS = System.Label.ME_Business_Unit_Clinical_Workplace_Solution;
        STRING GD_ME_SURGERY = System.Label.ME_Business_Unit_Surgery;
        STRING GD_ME_DIALYSIS = System.Label.ME_Business_Unit_Dialysis;
        STRING GD_ME_IMAGING_SYSTEM = System.Label.ME_Business_Unit_Imaging;
        STRING GD_ME_PHARMACY_AUTOMATION = System.Label.ME_Business_Unit_Pharmacy_Automation;
        system.debug('GD_ME_GI_ET ' + GD_ME_GI_ET);
            List<Approval.ProcessSubmitRequest> approvalRequestList = new List<Approval.ProcessSubmitRequest>();
            String approverId;
            boolean toUpdate = false;
            system.debug('Business Unit Name '+quoteRecord.GD_Business_Unit__r.Name);
            if(quoteRecord.GD_Business_Unit__r.Name == GD_ME_SURGERY || quoteRecord.GD_Business_Unit__r.Name == GD_ME_INFECTION_CONTROL || 
               quoteRecord.GD_Business_Unit__r.Name == GD_ME_CWS || quoteRecord.GD_Business_Unit__r.Name == GD_ME_GI_ET || 
               quoteRecord.GD_Business_Unit__r.Name == GD_ME_DIALYSIS || quoteRecord.GD_Business_Unit__r.Name == GD_ME_IMAGING_SYSTEM || 
               quoteRecord.GD_Business_Unit__r.Name == GD_ME_PHARMACY_AUTOMATION || quoteRecord.GD_Business_Unit__r.Name == GD_ME_CRITICAL_CARE){
                   toUpdate = true;
                system.debug('*** Entered ***');
                Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
                if(approvalRequestList.size() >0){
                //} removed 

                Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
                id submitterId;
                for(ProcessInstanceWorkitem pItem : [Select p.Id,p.ActorId,p.ProcessInstance.SubmittedById from ProcessInstanceWorkitem p where p.ProcessInstance.TargetObjectId =: quoteRecord.Id]){
                    system.debug('pItem *** ' + pItem);
                system.debug('submitterId *** ' + submitterId);
                    list<User> userDetails =[SELECT Id, Name, Profile.Name, UserRole.Name FROM User where Id=:submitterId];
                    system.debug('userDetails *** ' + userDetails);
                    if(userDetails!=null && userDetails.size()>0 && (userDetails[0].Profile.Name=='GD ME Sales Coordinator' || userDetails[0].Profile.Name=='GD ME Sales Rep' || userDetails[0].Profile.Name=='GD ME Sales Manager')){
                        req.setComments('Approving request for' + quoteRecord.Name);
                        req.setAction('Approve'); //Approve Record 
                        system.debug('req *** ' + req);
                        Approval.ProcessResult result = Approval.process(req);
                        system.debug('result *** ' + result);
                    system.debug('**** Not Matched with Critiria ****');
        }catch(Exception e){
            system.debug('EXCEPTION ******* '+e.getStackTraceString());
            throw new AuraHandledException(e.getMessage());    

Thanks in advance.

Best Answer chosen by Soundar Raj Ponpandi
AbhishekAbhishek (Salesforce Developers) 
This can be achieved by:

    Creating a formula field at the quote level which makes use of the $User notation (& accessing their name/username).
    To exclude the Sales Ops team from select rules, add an additional condition to the rules that say the field in point #1 above is not equal to the Sales Ops individuals username or name.

Note that if there are only a few Sales Ops members the names can be separated with commas in one approval condition, but if it's a large team it should be separated into multiple conditions (one per individual).  The conditions med on the approval rule will need to be adjusted if it was set to Any previously to Custom w/ Advanced Condition populated.

For further reference, you can try this (https://wdcigroup.net/salesforce-tips-skipping-approval-process/).

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.