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
Siana DcruzSiana Dcruz 

How to avoid recursion in trigger

Hey all,
I have a trigger something like this:

trigger ClaimData on Intake__c (after update) {

 Profile p = [ SELECT Name FROM Profile WHERE Id =: UserInfo.getProfileId() ];
if (p.Name != 'Data Manager') {
        List<Claim__c> claims = new List<Claim__c>();
        List<Id> intakeIds = new List<Id>();
        List<Claim__c> claimsToUpdate = new List<Claim__c>();
        for (Intake__c i : Trigger.new){
                intakeIds.add(i.Id);
        }
            
        claims = [SELECT fld1,fld2,fld3....,fldn from claim__c where intake__c IN: intakeIds];
        
        List<recordType> recordTypes = [select id,name from recordType where SobjectType ='claim__c' And name in ('WC','AL','PROP','GL','MR')];     
        Map<String,recordType> mapRecordTypes = new Map<String,recordType>();
        Map<Id, List<Claim__c>> claimMap= new Map<Id, List<Claim__c>>();
        
        for (recordType r: recordTypes) {
            mapRecordTypes.put(r.DeveloperName, r);
        }
        
        for (Claim__c c: claims) {
            List<Claim__c> int2Clms = claimMap.get(c.intake__c);
            if(int2Clms == null){
                claimMap.put(c.intake__c,int2Clms = new List<Claim__c>());
            }
            int2Clms.add(c);
        }
        
        for(Intake__c i : Trigger.new){
   List<Claim__c > claimsList = claimMap.get(i.Id);
                //1
                 for(Claim__c c:claimsList){
               c.fld1='value1';
        c.fld2='value2';
                 .
         .
         .    
         .        
         .
   }
}
}
}

Since this is an after update trigger and I am updating records with an external dml,it might lead to recurssion.Can anyone
please tell me how to avoid recursion in my trigger.Any help would be greatly appreciated.
Thanks!!
Shweta_AgarwalShweta_Agarwal
Hi Siana

kinldy refere this you will get some idea to process further.
https://help.salesforce.com/apex/HTViewSolution?id=000133752&language=en_US

Hope it will help you....

Thanks
Shweta
karthikeyan perumalkarthikeyan perumal
Hello, 

There are some ways you can avoid Recursive 
Using Trigger Context Variable in your Trigger. 
Before Update or Insert. Make sure your are using Trigger Context Variable. like Trigger.isInsert,Trigger.isupdate according to you consition, 
also you can use Static Boolean or Static variable to mark sure you trigger run once. 
Here is the Example. 

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_static.htm

https://help.salesforce.com/apex/HTViewSolution?id=000133752&language=en_US

Hope it will Help you.

Thanks
karthik