You need to sign in to do that
Don't have an account?
Shobhit Jain 7
After Insert Trigger Issue
Could someone throw light on this what could be the issue in this trigger? JFYI it gives below error
Apex trigger t2 caused an unexpected exception, contact your administrator: t2: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, t2: maximum trigger depth exceeded Case trigger even
trigger t2 on Case (after insert)
{
List<case> chcase = new List<case>();
for (Case c: trigger.New)
{
Case child = new Case(ParentId = c.Id, subject = c.Subject);
chcase.add(child);
}
insert chcase;
}
Apex trigger t2 caused an unexpected exception, contact your administrator: t2: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, t2: maximum trigger depth exceeded Case trigger even
trigger t2 on Case (after insert)
{
List<case> chcase = new List<case>();
for (Case c: trigger.New)
{
Case child = new Case(ParentId = c.Id, subject = c.Subject);
chcase.add(child);
}
insert chcase;
}
As @bryan pointed out, this will result in an infinite loop of cases trying to create child cases.
Agreeing with @Bryan and @Hargobind, your trigger will result in an Infinite loop of cases and you can create and update a Flag on your Original case as "Parent Case" to distinguish with other cases.
Another workaround is, you can check the case whether it has a ParentId or not before continuing in the loop to create a Child Case.
Please find below modified trigger.
Best Regards,
BALAJI