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
Shruthi MN 88Shruthi MN 88 

Automate approval process on Quote

Can you help complete the below code along with trigger
 

  3. Automate approval process on Quote ( created in previous tasks ) using trigger.

Check approval criteria in trigger.



public class ApprovalProcessHandler {
    public static void approval(){
        Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
req.setObjectId('0055j000009sbROAAY');
//If the next step in your approval process is another Apex approval process, you specify exactly one user ID as the next approver. 
//If not, you cannot specify a user ID and this method must be null.
//req.setNextApproverIds(null);
Approval.ProcessResult processResult = Approval.process(req);
//Valid values are: Approved, Rejected, Removed or Pending.
System.assertequals('Draft', + processResult.getInstanceStatus());
    }
    
}
AshwiniAshwini (Salesforce Developers) 
Hi Shruthi,
Can you elaborate more on the ask?
Are you looking for trigger logic for the above handler class?
Shruthi MN 88Shruthi MN 88
I have an approval process already created I want to aumtomate that in trigger and help me with the trigger as well. This code is not working:. I am getting the attached error

trigger ApprovalProcessHandlerTrigger on Quote ( after insert,after update) {
 ApprovalProcessHandler.approval();

}

User-added image
bla bling2bla bling2
This application is quite useful
connect 4 (https://connect-4.io)
AshwiniAshwini (Salesforce Developers) 
Hi Shruthi,
You can refer below trigger logic and make adjustments as per your requirement.
trigger ApprovalProcessHandlerTrigger on Quote (after insert, after update) {
    // to get list of Quotes that needs approval
    List<Quote> quotesForApproval = new List<Quote>();

    for (Quote quote : Trigger.new) {
        // Check if the Quote meets your approval criteria
        if (quote.Amount > 10000 && quote.Status == 'Draft') {
            quotesForApproval.add(quote);
        }
    }

    if (!quotesForApproval.isEmpty()) {
        // Creating an approval request for each Quote
        List<Approval.ProcessSubmitRequest> approvalRequests = new List<Approval.ProcessSubmitRequest>();

        for (Quote quote : quotesForApproval) {
            Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
            req.setObjectId(quote.Id);

            // Adding approval request to the list
            approvalRequests.add(req);
        }

        // Submitting the approval requests
        List<Approval.ProcessResult> processResults = Approval.process(approvalRequests);

        for (Approval.ProcessResult processResult : processResults) {
            // Check the approval status and handle as needed
            if (processResult.isSuccess()) {
                System.debug('Quote submitted for approval successfully.');
            } else {
                System.debug('Quote approval submission failed: ' + processResult.getErrors()[0].getMessage());
            }
        }
    }
}
Related:https://www.jitendrazaa.com/blog/salesforce/dynamic-approval-process-based-on-the-apex-and-trigger/
If this information helps, please mark the answer as best. Thank you