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

Create Parent and Child Cases in trigger


I have a trigger where I need to create two sets of cases, parent cases and child cases, and relate them automatically.

Say I need to create 3 cases: 1 parent, 2 childs; when the trigger fires, it creates the 2 child cases and stores them in a List <Case> and then it creates the parent case and stores all 3 of them in a Map <Case, List<Case>>.

Obviously, I have no IDs since they're new records, that's why I use the map to be able to relate the records after I insert them. 

The map is created perfectly; the problem happens when I try to insert the records. First I insert the parent cases (in Map.keyset()), to be able to retrieve the IDs and assign them to the child cases (in Map.values()). After I insert the records in Map.keyset(), for some strange reason I "lose" all records in Map.values().

Don't understand why it's happening... does anyone have any idea? 

I know I've done something similar to this before, not necessarilly parent and child situation, but inserting records of primary object, relating primary object to secondary object and inserting records of secondary object. And it worked, but both of them were custom objects, so I don't know if there's a difference there. 

Thanks in advance, 


Hi Ramu_SFDC, 

I took a different aproach to this issue, and I was able to workround it and create the parent & child cases using a "Fake Parent ID" generated randomly in my code. 

But the link for the post you sent had another link ( about Foreign Keys (1st time I heard of it actually), very interesting, and I'm going to take a closer look at it and see if I can use it to optimize my code. 

So thank you for the time and help. Have a good one.