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
babloo123babloo123 

Apex Trigger on one Object updates other Object but other Object has same update event and being called

I have a trigger on custom objectA that updates custom Object B but I have another trigger on Custom Object B on update Event but when I run trigger 1 the trigger 2 is being automatically called due to the object B being same. Can some one guide how to prevent this.
Lam CorporationLam Corporation
Hi 
I presume your trigger on Custom object B meeting the criteria hence firing?
It would help if you included some code for review. Thanks
babloo123babloo123
Trigger Handler Below
------------------
//This Trigger is used to automatically create COI and update TechnicalAbstract from RAObject to COI
trigger ResearchApplicationTriggers on Research_Application__c (after insert, after update) {
     ResearchApplicationTriggerClass handler = new ResearchApplicationTriggerClass();
    List<Research_Application__c> ra= new List<Research_Application__c>();
   for(Research_Application__c rap:trigger.new){
       ra.add(rap);
        }
       if(ra.size()>0){
     if(Trigger.isInsert && Trigger.isAfter){
         system.debug(ra);
     handler.CreateCOIautomatically(ra);  
       }
    
if(Trigger.isUpdate && Trigger.isAfter)
    {
      handler.UpdateCOItechnicalabstract(ra);
    
}
       }
  
}
Trigger Handler Class
-----------------------------------------------------------------------------------------------
//This is a TriggerHandler Class that handles the triggers on ResearchApplication Object
public class ResearchApplicationTriggerClass {
public ResearchApplicationTriggerClass(){}
        //This method creates the COI automatically for all reviewers in the panel assignment for that particular panel.
    public void createCOIautomatically(list<Research_Application__c> rap)
    {
      //Below Soql query gets the list of Reviewers assigned in the pane assignments for that particular Panel  
        for(integer k=0;k<rap.size();k++){
        list<panel_assignment__c> lp=[select Reviewer_Name__c from panel_assignment__C where panel__C in (select id from panel__c where id =: rap[k].panel__C )];
system.debug('The Reviewers are' + lp);
            //Take COI recors to a list to not hit Governor Limits
      list<COI_Expertise__c> coilist= new list<COI_Expertise__c>();
        for (integer i=0;i<lp.size();i++)
    {
      
  system.debug('Entering'+lp[i].Reviewer_Name__c);
  COI_Expertise__c  c=new COI_Expertise__c ();
         //Initialize and assign the the COI object with the fields we require
        c.Research_Application__c= rap[k].Id;
        system.debug('The rap Id is' +rap[k].Id);
        system.Debug(c.Research_Application__c);
        c.Technical_Abstract__c=rap[k].Technical_Abstract__c;
         c.Reviewer_Name__c = lp[i].Reviewer_Name__c;
      system.debug(lp[i].Reviewer_Name__c);
         user u=[select id,firstname,lastname from user where contactID=:lp[i].Reviewer_Name__c];
     Id  usrid=string.valueof(u.id);
        system.debug('USer ID:' + u.id + 'Reviewer ID' + lp[i].Reviewer_Name__c);
        //Assign the owner of the COI to be the same as Reviewer Name
          c.Ownerid = usrid;
         coilist.add(c);
             } 
            insert coilist;
        }   
    }
    
    //This method copies the technical abstract on Research Application to the COI associated to it.
        public void updateCOItechnicalabstract(list<Research_Application__c> rapl)
    {
        //Below is the soql query to get the technical abstract from that particular research application
        list<COI_Expertise__c> coi= new list<COI_Expertise__c>();
        for(integer j=0;j<rapl.size();j++){
//Below is the soql query to get the COI records from that particular research application
 list<COI_Expertise__c> c=[select id,technical_abstract__c from COI_Expertise__c where Research_Application__r.Id=:rapl[j].Id];
  //Assign the technical abstract from RA to respective COI's
    for(COI_Expertise__c ci:c)
    {
   ci.Technical_Abstract__c=rapl[j].Technical_Abstract__c;
    coi.add(ci);
    }
     update coi;       
    }
        }
}


Another Trigger this is the one firing when checking trigger one

Trigger 2

//This Trigger send mail to the MRO if COI is marked unclear
trigger SendEmailtoMRO on COI_Expertise__c (after update) {
    string text1='Unclear if COI Exists';

    
  //Below is the email that is sent based on the condition and the email sent is from the template
    for(COI_Expertise__c ci:trigger.new){
  if (ci.Conflict_of_Interest__c.equals(text1)){
 Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
 mail.setTargetObjectId(ci.Test__c);
  mail.setSenderDisplayName('PCORI');
  mail.setUseSignature(false);
  mail.setBccSender(false);
  mail.setSaveAsActivity(false);
    //checking condition
 
 
   //Template to select from the list of email templates available
          EmailTemplate et=[Select id from EmailTemplate where DeveloperName=:'Unclear_if_COI_Exists'];
     system.debug('The ETID is' + et);
          mail.setTemplateId(et.id);
          Messaging.SendEmailResult [] r = 
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});   
     system.debug(r);
      } 
        }
        }
Lam CorporationLam Corporation
Does the field Conflict_of_Interest__c get updated with 'Unclear if COI Exists' when these triggers fire together?
 
babloo123babloo123
Basically the first trigger is updating COI object and second trigger is on COI object with event after update .

Order when I update a field on first trigger on 1st Object same should be copied onto the  field of second object(COI) and it is should update COI

but I have a trigger on COI(after update) so when ever the first object is getting updated it is trying to update second and second object having update event is firing and not letting be update first object.

Conflict on Interest being on seconf trigger works if both triggers activated. My Issue on First trigger I posted not updating and calling the second trigger. How to avoid this situation can you please guide me which can be of great help.