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
sandhya santhanagopalansandhya santhanagopalan 

DEV 401 trigger question-help needed

A developer has the following trigger that fires after insert and creates a child Case whenever a new Case is created.
List<Case> childCases = new List<Case>();
for ( Case parent : Trigger.new )
Case child = new Case(Parentid = parent.id, Subject = parent -Subject);
childCases.add( child );
insert childCases;
What happens after the code block executes?

A.multiple child cases are created for each parent in trigger.new
B.Trigger enters infinite loop and eventually fails
C.Child case is created for each parent case in trigger.new
D.The trigger fails if subject field on parent is blank
Best Answer chosen by sandhya santhanagopalan
Greg CooganGreg Coogan
On the line that creates a child Case record, the parameter that sets "Subject" seems to have invalid syntax.

Case child = new Case(Parentid = parent.id, Subject = parent -Subject);

Did you mean to type the below line instead?
Case child = new Case(Parentid = parent.id, Subject = parent.Subject);

If that was just a typo, lets break down each answer choice:
A.multiple child cases are created for each parent in trigger.new - This cannot be the answer since the For loop only creates a single child case in the For loop.
B.Trigger enters infinite loop and eventually fails - The trigger runs when a new case is created, and creates a case within itself. This recursive behavior would result in an infinite loop with this particular code.
C.Child case is created for each parent case in trigger.new - This is true, but choice B is the better answer since the code would fail.
D.The trigger fails if subject field on parent is blank - This cannot be possible since the parent should be able to be created without a Subject if that field is required.

All Answers

Greg CooganGreg Coogan
On the line that creates a child Case record, the parameter that sets "Subject" seems to have invalid syntax.

Case child = new Case(Parentid = parent.id, Subject = parent -Subject);

Did you mean to type the below line instead?
Case child = new Case(Parentid = parent.id, Subject = parent.Subject);

If that was just a typo, lets break down each answer choice:
A.multiple child cases are created for each parent in trigger.new - This cannot be the answer since the For loop only creates a single child case in the For loop.
B.Trigger enters infinite loop and eventually fails - The trigger runs when a new case is created, and creates a case within itself. This recursive behavior would result in an infinite loop with this particular code.
C.Child case is created for each parent case in trigger.new - This is true, but choice B is the better answer since the code would fail.
D.The trigger fails if subject field on parent is blank - This cannot be possible since the parent should be able to be created without a Subject if that field is required.
This was selected as the best answer
jonghoon baejonghoon bae
To Greg.
Thank you for your great help. How do we prevent to have an infinite loop in this case?
Austin Wang 13Austin Wang 13
To jonghoon bae
To Avoid infinite loop, you have to check parent.id == null so a child case won't create another child case


List<Case> childCases = new List<Case>();
for (Case parent : Trigger.new){
if(case.ParentID == null) { // it means it is a parent case 
Case child = new Case(ParentId = parent.Id, Subject = parent.Subject);
childCases.add(child);
}
}
insert childCases;
Sourajit MohantySourajit Mohanty
To jonghoon bae
To Avoid infinite loop, you have to check parent.id == null so a child case won't create another child case

List<Case> childCases = new List<Case>();
for (Case parent : Trigger.new){
if(parent.ParentID== null) { // it means the current case ie. the one which fired the trigger is the parent case 
Case child = new Case(ParentId = parent.Id, Subject = parent.Subject);
childCases.add(child);
}
}
insert childCases;