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
Babar Hussain 4Babar Hussain 4 

Approval rejection

Hi Friends can anyone please help me in this case. I need to update the Task Owner, Task Status & Case Owner if the approval rejected the process.  Please modify the apex code according to it and share to me that is very helpful to me & I'm verypoor in coding.User-added imageUser-added image
Please find the attached screenshots & Apex Code 

Apex Code :
public without sharing class CaseTriggerProcurementHandler {
   
    public static void handleprocurementCaseAfterInsert(List<SObject> newSobjs)
    {
        //Turn SObject list to case list
        List<Case> newCases = (List<Case>) newSobjs;
       
        //Get RT name, to match to the cmdt
        Map<ID,Schema.RecordTypeInfo> rt_Map = Case.sObjectType.getDescribe().getRecordTypeInfosById();
        //Create empty list of tasks to be inserted
        Task[] tskList = new List<Task>();
       
        //Create empty var to store the mapping of tasks that match the case record type
        List<Procurement_Case_Task_Settings__mdt> recordTypeSpecificMap = new List<Procurement_Case_Task_Settings__mdt>();
       
        //Will contain the list of tasks to be created if case details match the conditional logic
        List<Procurement_Case_Task_Settings__mdt> conditionalMap = new List<Procurement_Case_Task_Settings__mdt>();
       
        //Get case record type/task mappings
        Procurement_Case_Task_Settings__mdt[] fullTaskMapping = [Select Id, Task_Owner__c,Task_Record_Type__c, Task_Subject__c, Task_Status__c, Case_Record_Type__c, Task_Type__c, Label, Task_SLA_Days__c, Conditional_Creation__c
                                                                 FROM Procurement_Case_Task_Settings__mdt];
       
        Id itRecTypeId = Schema.SObjectType.Case.getRecordTypeInfosByDeveloperName().get('Procurement_IT').getRecordTypeId();
       
        Boolean buyDeskTaskCreated = false;
        Boolean projectTypePOSCT = false;
       
        //Loop through all cases in trigger
        for(case u : newCases){
           
            //Loop through the full task mapping
            for(Procurement_Case_Task_Settings__mdt fs : fullTaskMapping){
               
                //For the case record type, find all matching task mappings and add to a list
                If (fs.Case_Record_Type__c == rt_map.get(u.recordTypeID).getDeveloperName()){
                    recordTypeSpecificMap.add(fs);
                }
            }
           
            //Loop through specific mapping
            for(Procurement_Case_Task_Settings__mdt sm : recordTypeSpecificMap){
               
                //Create tasks with unconditional creation logic (Specified in the cmdt mapping)
                If (!sm.Conditional_Creation__c && u.Project_Type__c != 'PO/SCT Approval Only'){
                    Task tsk = New Task(
                        RecordTypeId = sm.Task_Record_Type__c,
                        Subject = sm.Task_Subject__c,
                        status = sm.Task_Status__c,
                        Type = sm.Task_Type__c,
                        OwnerId = sm.Task_Owner__c,
                        WhatId = u.id,
                        ActivityDate = date.today() + integer.valueof(sm.Task_SLA_Days__c));
                    tskList.add(tsk);
                } else {
                   
                    /**************************************
                    CONDITIONAL CREATION LOGIC
                    For all tasks where the CMDT mappng's Conditional_Creation__c is TRUE, check whether the case details match creation requirements
                    ***************************************/
                    //Create Buy Desk Review for non-IT case?
                    /*If (u.RecordTypeId != itRecTypeId &&
                        sm.Task_Type__c == 'Buy Desk Review' &&
                        u.Total_Expected_Cost_for_Duration__c < 250000 &&
                        u.Procurement_System__c == 'Ariba' &&
                        (u.Region__c == 'North America' || u.Region__c == 'EANZ')){
                        conditionalMap.add(sm);
                        buyDeskTaskCreated = true;
                    }*/
                   
                    //Create GSM LCM Review for non-IT case?
                    /*If (u.RecordTypeId != itRecTypeId &&
                        sm.Task_Type__c == 'GSM LCM Review' &&
                        !(u.Total_Expected_Cost_for_Duration__c < 250000 &&
                        u.Procurement_System__c == 'Ariba' &&
                        (u.Region__c == 'North America' || u.Region__c == 'EANZ'))){
                        conditionalMap.add(sm);
                    } */    
                   
                    //Create Buy Desk Review for IT case?
                    If (//u.RecordTypeId == itRecTypeId &&
                        sm.Task_Type__c == 'Buy Desk Review' &&
                        u.Total_Expected_Cost_for_Duration__c < 250000){
                        conditionalMap.add(sm);
                           
                       buyDeskTaskCreated = true;
                    }
                   
                    //Create GSM LCM Review for IT case?
                    If (//u.RecordTypeId == itRecTypeId &&
                        sm.Task_Type__c == 'GSM LCM Review' &&
                        !(u.Total_Expected_Cost_for_Duration__c < 250000)){
                        conditionalMap.add(sm);
                    }
                   
                   
                   
                    //Create Trade Compliance Review? removed PO/SCT Approval Only
                    list <String> tcProjTypes = new List <String> {'Proof of Concept', 'New Contract', 'Sourcing Event (RFQ, RFP, RFI)'};
                        If (sm.Task_Type__c == 'Trade Compliance Review' && tcProjTypes.Contains(u.Project_Type__c)){
                            conditionalMap.add(sm);
                        }
                   
                    //Create Supplier Risk Review? 06/02:removed 'Amendment/SOW'
                    list <String> srProjTypes = new List <String> {'Contract Renewal/Renegotiation', 'Proof of Concept', 'New Contract', 'Sourcing Event (RFQ, RFP, RFI)'};
                        system.debug(srProjTypes);
                    If (sm.Task_Type__c == 'Supplier Risk Review' && srProjTypes.Contains(u.Project_Type__c)){
                        conditionalMap.add(sm);
                    }
                   
                    //Create Supplier Divesity Review?
                    list <String> sdProjTypes = new List <String> {'Amendment/SOW', 'Proof of Concept', 'New Contract', 'Sourcing Event (RFQ, RFP, RFI)'};
                        If (sm.Task_Type__c == 'Supplier Diversity Review' && sdProjTypes.Contains(u.Project_Type__c)){
                            conditionalMap.add(sm);
                        }
                   
                    //Create Sourcing Council Review?
                    If (sm.Task_Type__c == 'Sourcing Council Review' && (u.Total_Expected_Cost_for_Duration__c >= 500000)){
                        conditionalMap.add(sm);
                    }
                }
            }
           
            //Create tasks with conditional creation logic
            for(Procurement_Case_Task_Settings__mdt cMap : conditionalMap){
                Task tsk = New Task(
                    RecordTypeId = cMap.Task_Record_Type__c,
                    Subject = cMap.Task_Subject__c,
                    status = cMap.Task_Status__c,
                    Type = cMap.Task_Type__c,
                    OwnerId = cMap.Task_Owner__c,
                    WhatId = u.id,
                    ActivityDate = date.today() + integer.valueof(cMap.Task_SLA_Days__c));
                tskList.add(tsk);
            }
        }
       
        //Scott 05/06/22 If Buy Desk Review Task is created, then Data Privacy Review and Insurance Review should also be routed to Buy Desk Team, not to LCM GSM Team
        if(buyDeskTaskCreated == true){
            for(task tsk : tskList){
                if(tsk.Type == 'Data Privacy Review' || tsk.Type == 'Insurance Review' ){
                    tsk.OwnerId = '00G4x000000S8Wj'; //buydesk queue
                }
            }
        }
       
        //Insert List of all tasks
        Insert tskList;
    }
   
    public static void handleprocurementCaseBeforeUpdate(List<SObject> newSobjs, List<SObject> oldSobjs)
    {
        //Turn SObject list to case list
        List<Case> newCases = (List<Case>) newSobjs;
        List<Case> OldCases = (List<Case>) oldSobjs;
        Map<Id, Case> mapOfExistingRecords = new Map<Id, Case>();
        Set<Id> setOfCaseId = new Set<Id>();
        List<Task> listOfTasks = new List<Task>();
        List<Task> listOfColsedTasks = new List<Task>();
       
         List<ProcessInstance > listOfProcessInstance  = new List<ProcessInstance>();
        List<ProcessInstance > listOfClosedProcessInstance  = new List<ProcessInstance>();
        for(Case c : OldCases)
        {
            mapOfExistingRecords.put(c.Id, c);
        }
       
        for(Case cse : newCases)
        {
            Case oldcase = (Case)mapOfExistingRecords.get(cse.Id);
           
            if(cse.Status == 'Closed' && oldcase.Status != 'Closed')
            {
                setOfCaseId.add(cse.Id);
            }
        }
       
        If(!setOfCaseId.isEmpty())
        {
            listOfTasks = [Select id,Status from Task where WhatId IN:setOfCaseId];
            listOfProcessInstance = [SELECT TargetObjectId, Status, Id FROM ProcessInstance where TargetObjectId IN:setOfCaseId
                                         ORDER BY CreatedDate DESC
                                         Limit 1];
            system.debug('listOfProcessInstance-->'+listOfProcessInstance);
            if(!listOfTasks.isEmpty())
            {
                for(Task t : listOfTasks)
                {
                    if(t.Status == 'Completed'  || t.Status == 'No Longer Required')
                    {
                        listOfColsedTasks.add(t);
                    }
                }
            }
           
            /*if(!listOfProcessInstance.isEmpty())
            {
                for(ProcessInstance p : listOfProcessInstance)
                {
                    if(p.Status == 'Approved')
                    {
                        listOfClosedProcessInstance.add(p);
                    }
                }
            }*/
           
        }
       
        for(Case cse : newCases)
        {
            Case oldcase = (Case)mapOfExistingRecords.get(cse.Id);
            if(cse.Status == 'Closed' && oldcase.Status != 'Closed')
            {
                if(!listOfTasks.isEmpty())
                {
                   if(listOfTasks.Size()!=listOfColsedTasks.size())
                   {
                       cse.addError('Please close all the taks before you close the case');
                       
                   }
                }
               
                if(!listOfProcessInstance.isEmpty())
                {
                    for(ProcessInstance p : listOfProcessInstance)
                    {
                        if(p.Status == 'Pending')
                        {
                            cse.addError('Please complete all the approval before you close the case');
                        }
                    }
                }
               
                   
            }
        }
    }
}
 
PriyaPriya (Salesforce Developers) 

Hey Babar,

Where is the issue you are facing? Have you tried changing the code as the requirement?

Regards,

Priya Ranjan