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
Rahul Bhattad 4Rahul Bhattad 4 

Stop for loop from executing in trigger

Hello,

We have two objects in our org Custom1__c and Approver__c which are linked via junction object Approval__c. I  want to build a logic in trigger on Approval Object(after update), which checks the the status(field on Approval) of previous stage approval record(s) and if they are Approved then call the future callout class with details of Approval records of next stage and if the last stage is approved then update therelated Custom1 record's Status field to Active. 

There can be multiple stages of Approvers and each stage can have 1 or 2 Approvers depending on the country.
Some part of my Trigger:
if(trigger.isAfter && trigger.isUpdate){
        Set<Id> MainOfferId = new Set<Id>();
        Integer counter1 = 0;
        Integer counter2 = 0;
        Integer counter3 = 0;
        Map<ID,Approval__c> Approval = new Map<ID,Approval__c>([Select Status__c, Stage__c, Approver_Name__c, Main_Offer_Name__c from Approval__c
                                                               WHERE Stage__c = 1 AND Id =:Trigger.newMap.keySet()]);
        
        for(Approval__c app : Approval.values())
        {
            if(app.Main_Offer_Name__c != null){
                MainOfferId.add(app.Main_Offer_Name__c);
            }
        }
        
        for(Id MOId : MainOfferId){
        List<Approval__c> lstapproval1 = [Select Id from Approval__c WHERE Main_Offer_Name__c =:MOId AND Stage__c = 1];
        List<Approval__c> lstapproval2 = [Select Id from Approval__c WHERE Main_Offer_Name__c =:MOId AND Stage__c = 2];
        List<Approval__c> lstapproval3 = [Select Id from Approval__c WHERE Main_Offer_Name__c =:MOId AND Stage__c = 3];
        
        if(lstapproval1.size()>0){
        for(Approval__c app : lstapproval1)
        {
            if(app.Status__c == 'Approved')
            {
                counter1 = counter1+1;
            }
        }
            if(counter1 == lstapproval1.size())
            {
                if(lstapproval2.size()>0){
                    Set<Id> SecondAppIds = new Set<Id> (new Map<Id,SObject>(lstapproval2).keySet());
                    CreateApprovalRequestAzur.createRequest(SecondAppIds);
                }
                else
                {
                    CameleonCPQ__Custom1__c MO = [Select Id, Status__c from CameleonCPQ__Custom1__c WHERE ID=:MOId];
                    MO.Status__c = 'Active';
                    
                }
            }
            
        }
            
        if(lstapproval2.size()>0){
            for(Approval__c app : lstapproval2){
                if(app.Status__c == 'Approved')
                {
                    counter2 = counter2+1;
                }
            }
             if(counter2 == lstapproval2.size())
             {
                 if(lstapproval3.size()>0)
                 {
                     Set<Id> ThirdAppIds = new Set<Id> (new Map<Id,SObject>(lstapproval3).keySet());
                     CreateApprovalRequestAzur.createRequest(ThirdAppIds);
                 }
                 else
                {
                    CameleonCPQ__Custom1__c MO = [Select Id, Status__c from CameleonCPQ__Custom1__c WHERE ID=:MOId];
                    MO.Status__c = 'Active';
                    
                }
             }
        
        }
            
        }
If 2 stages are approved and 3rd stage Approval record is approved, then according this code, 2nd Stage Approval records will be validated again and will again call the future callout class 'CreateApprovalRequestAzur' with detials for 2nd stage Approval. I don't want this to happen. It should only check 3rd Stage directly.