You need to sign in to do that
Don't have an account?
Paul 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!
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!
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
All Answers
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 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
}