You need to sign in to do that
Don't have an account?
Trigger to create Case
Hi, I have created the below trigger using code from the Cookbook.
trigger NewCase on Service_Reports__c (after insert) { List<Case> cases = new List<Case>(); for (Service_Reports__c newCase: Trigger.New) { if (newCase.Name != null) { cases.add(new Case(Subject = 'Remedial Case', Type = 'Remedial Work')); } } insert cases; }
I need some help however on making sure that when the case is created, it is linked back to the service report. At the moment it just creates a new case and doesnt relate to the Service Report in the Case's Service Report lookup! Of course I understand thats because I havent told it to, but I am struggling to figure out how to get it to!!!
Can anyone help?
Thanks!
I don't know what's the name of the Service Reports lookup field on the case object but if we assume that's calle "Service_ReportId__c" then your code should look as follows:
trigger NewCase on Service_Reports__c (after insert) {
List<Case> cases = new List<Case>();
for (Service_Reports__c sReport: Trigger.New) {
if (sReport.Name != null) {
Case c = new Case();
c.Subject = 'Remedial Case';
c.Type = 'Remedial Work';
c.Service_ReportId__c = sReport.Id;
cases.add(c);
}
}
insert cases;
}