• Gauthami Byreddy 8
  • NEWBIE
  • 0 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies
Below is the following code that needs to be modified as it contains a DML statement inside of FOR loop which isn't a good practice. Anyone here to help me out?

trigger IncidentTrigger on Trust_Incident__c (before update) {
     Trust_Incident_History__c tih = new Trust_Incident_History__c();
     for(Trust_Incident__c newInc : Trigger.New) {
        
        Trust_Incident__c oldInc =  Trigger.oldMap.get(newInc.id);
        
        if(newInc.Status__c != oldInc.Status__c || newInc.Description__c != oldInc.Description__c) {
           // Trust_Incident_History__c tih = new Trust_Incident_History__c();
            tih.Description__c = oldInc.Description__c;
            tih.Status__c = oldInc.Status__c;
            tih.Incident__c = newInc.id;
            tih.Name = newInc.Name;
            tih.ModifiedUser__c = oldInc.LastModifiedById;
            tih.LastUpdate__c = oldInc.LastModifiedDate;
            insert tih; 
        }   
    }
 
}

Thank You!