Approve or Reject a Record using Screen Flow and LWC and Quick Action #inSalesforce

Approve or Reject a Record using Screen Flow and LWC and Quick Action  #inSalesforce


approvalLWC


html:

<template>
    <lightning-record-view-form object-api-name={objectApiName}
                                record-id={recordId}>
        <div class="slds-box">
             
            <lightning-output-field field-name="Name"> </lightning-output-field>
             
            <lightning-output-field field-name="Close Date"> </lightning-output-field>
             
            <lightning-output-field field-name="Stage"> </lightning-output-field>
        </div>
    </lightning-record-view-form><br/>
    <lightning-button variant="brand" label="Approve" title="Approve leave request" onclick={handleApprove} class="slds-m-left_x-small"></lightning-button>
    <lightning-button variant="destructive" label="Reject" title="Reject leave request" onclick={handleReject} class="slds-m-left_x-small"></lightning-button>
</template>


js:

import { LightningElement,api } from 'lwc';
import opp_OBJECT from '@salesforce/schema/Opportunity';
 
import approveReject from '@salesforce/apex/opportunityClass.approveRejectRequest';
 
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
 
export default class LeaveApplicationApproval extends LightningElement {
    @api recordId;
 
    objectApiName = opp_OBJECT;
 
    isRequestApproved = false;
 
    //handleApprove function to send the approve request to apex.
    handleApprove(){
        this.isRequestApproved = true;
        this.sendRequest();
    }
 
    handleReject(){
        this.sendRequest();
    }
    sendRequest(){
        approveReject({ recordIdStr: this.recordId, isApprove:this.isRequestApproved })
            .then((result) => {
 
                if(result == 'Approved'){
                     
                    this.dispatchEvent(
                        new ShowToastEvent({
                            title: "Success ! ",
                            message: 'The record has been approved!',
                            variant: "success"
                        })
                    );
                    
                }
                else if(result =='Rejected'){
                    
                    this.dispatchEvent(
                        new ShowToastEvent({
                            title: "Success ! ",
                            message: 'The record has been rejected!',
                            variant: "success"
                        })
                    );
                     
                }
                else{
                    
                     this.dispatchEvent(
                        new ShowToastEvent({
                            title: "Error ! ",
                            message: result,
                            variant: "error"
                        })
                    );
                     
                }
                 
                this.error = undefined;
            })
            .catch((error) => {
                 
                this.error = error;
             
                this.dispatchEvent(
                    new ShowToastEvent({
                        title: "Error ",
                        message: 'Record is already approved/rejected.',
                        variant: "error"
                    })
                );
            });
    }
 
}



xml:

<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>57.0</apiVersion>
    <isExposed>true</isExposed>
    <masterLabel>Leave Application Approval</masterLabel>
    <description>This is a opportunity approval component.</description>
  <targets>
      <target>lightning__FlowScreen</target>
      <target>lightning__RecordPage</target>
       
  </targets>
  <targetConfigs>
    <targetConfig targets="lightning__FlowScreen,lightning__RecordPage">
            <property name="recordId" type="String" label="Enter record id"/>
    </targetConfig>
  </targetConfigs>
</LightningComponentBundle>



class:

public with sharing class opportunityController {
    @AuraEnabled
    public static String approveRejectRequest(String recordIdStr, Boolean isApprove){
        try{
 
            String resultString = isApprove ? 'Approved': 'Rejected';
            
            Opportunity op = [Select OwnerId from Opportunity where id=:recordIdStr];
            User u = [SELECT ManagerID FROM User where id=:op.OwnerId];
            Boolean isCurrentUserManagerOfRecordOwner  = (u.ManagerID == UserInfo.getUserId() )? true:false;
             
            if(isCurrentUserManagerOfRecordOwner){
            
                ProcessInstance pi = [SELECT Id,Status,TargetObjectId FROM ProcessInstance where Status='Pending' and TargetObjectId = :recordIdStr];
 
                ProcessInstanceWorkitem piw =  [SELECT Id,ProcessInstanceId FROM ProcessInstanceWorkitem WHERE ProcessInstanceId =:pi.Id];
 
                Approval.ProcessWorkitemRequest req1 = new Approval.ProcessWorkitemRequest();
 
                req1.setComments('Your request has been approved!');
 
                if(isApprove){
                    req1.setAction('Approve'); 
                }else{
                    req1.setAction('Reject'); 
                }
                 
                req1.setNextApproverIds(new Id[] {UserInfo.getUserId()});
 
                req1.setWorkitemId(piw.Id);
 
                if(!Test.isRunningTest()){
                    Approval.ProcessResult result =  Approval.process(req1);
                }
 
                return resultString;
            }
            else{
                return System.Label.Current_user_is_not_manager_of_record_owner;
            }
        }
        catch(Exception e){ throw new AuraHandledException(e.getMessage()); }
    }
}



Create flow:

Create a variable recordId, text, available for input and output checked


Add screen:

Drag drop the LWC component created, right hand side- enter record id equals recordId


Create Quick Action on Opportunity:

Approve/Reject:

Fill up the details as per need and select the flow created and Save


Add the quick action to the Opportunity Page and Test it here clicking the Approve/Reject

Comments