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
Paul BachmannPaul Bachmann 

Need help writing Trigger

Hi, I need some help writing a trigger.
I have two objects I'll be dealing with, Case (standard object) and Injury__c (custom object).  Injury is set up as a related list on the Case record.  On Injury__c records I have a picklist with multiple injury types.  Two of these values are "Head" and "Face".  Also, there can be multiple Injuries, so could have just one, could have one of those and another injury (e.g., arm) or I could have both.
I have a Page Layout on Case specific to Head Injuries.
I need to write a trigger that looks through the Injury__c related list to see if any of the records have injury type records (value in the picklist) of "Head" or "Face".  If it does, then I need the Case Record Type changed to "Head Injury" Record Type.
I can't get my trigger attempts to work.

Thanks!
Best Answer chosen by Paul Bachmann
logontokartiklogontokartik
Hi Paul,

Maybe the requirement needs to be more clear, in the above scenario, you want to write trigger why? and when?

From what I understand, you want to write trigger on Injury__c which is basically a child to Case, so whenever any injury record is created or updated, it need to update the parent case's record type.

If my understanding is right, here is how I would write
 
trigger InjuryTrigger on Injury__c(after insert, after update) {
      List<Case> casesToUpdate = new List<Case>();
      String headRT = Case.SobjectType.getDescribe().getRecordTypeInfosByName().get('<RecordTypeName>').getRecordTypeId();
       for (Injury__c inj : Trigger.new) {
             if (inj.Injury_Type__c = 'Head' || inj.Injury_Type__c = 'Face') {
                Case updCase = new Case(Id=inj.Case__c,RecordTypeId=headRT); // This is build instance to update,
                casesToUpdate.add(updCase); 
            } 
      }  

   update casesToUpdate;

}



 

All Answers

logontokartiklogontokartik
Hi Paul,

Maybe the requirement needs to be more clear, in the above scenario, you want to write trigger why? and when?

From what I understand, you want to write trigger on Injury__c which is basically a child to Case, so whenever any injury record is created or updated, it need to update the parent case's record type.

If my understanding is right, here is how I would write
 
trigger InjuryTrigger on Injury__c(after insert, after update) {
      List<Case> casesToUpdate = new List<Case>();
      String headRT = Case.SobjectType.getDescribe().getRecordTypeInfosByName().get('<RecordTypeName>').getRecordTypeId();
       for (Injury__c inj : Trigger.new) {
             if (inj.Injury_Type__c = 'Head' || inj.Injury_Type__c = 'Face') {
                Case updCase = new Case(Id=inj.Case__c,RecordTypeId=headRT); // This is build instance to update,
                casesToUpdate.add(updCase); 
            } 
      }  

   update casesToUpdate;

}



 
This was selected as the best answer
Paul BachmannPaul Bachmann
So I am close, thanks for your help!  I'm now getting an error on my line 10(update casesToUpdate;) that says "Unexpected Token: Update"

trigger HeadInjuryTrigger on Injury__c(after insert, after update) {
      List<Case> casesToUpdate = new List<Case>();
      String headRT = Case.SobjectType.getDescribe().getRecordTypeInfosByName().get('Ski_Incident_w_Head_Injury').getRecordTypeId();
       for (Injury__c inj : Trigger.new) {
             if (inj.Injury_Area__c = 'Head') 
                Case updCase = new Case(Id=inj.Case__c,RecordTypeId=headRT);
                casesToUpdate.add(updCase);
            }
      } 
update casesToUpdate; // error happening here now
}