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
arnaud_carnaud_c 

Trigger on related case to update parent case

Hi all,

 

I am trying to create a trigger on a related case that would update a field on the parent case when the new related (child) case is created against the parent. I am not an expert at apex triggers but have modified some code. I have copied it below.

 

Can anybody help me complete the code for this to work.

 

Thanks

 

 

 

trigger UpdateParentCase on Case (after insert, after update){
Case relcase = Trigger.new[0];
Case ca = new Case();
for(Case c:[Select Id,  Attempted_Call__c from Case where
Id = :relcase.Parent limit 1]){
ca = c;
ca.Attempted_Call__c = true;
update ca;
}
}

Best Answer chosen by Admin (Salesforce Developers) 
TrueCloudTrueCloud

You are using Trigger.new[0]. This will only update the first Case record that was triggered. I am not sure if you want to do that. 

 

Here is an Example:

 

 

trigger UpdateParentCase on Case (after insert, after update){
Case relcase = Trigger.new;
Case ca = new Case();
for(Case c:[Select Id,  Attempted_Call__c from Case where
if (c != null) { // This will check if there is a Parent Case or associated or not.

Id = :relcase.Parent limit 1]){
ca = c;
ca.Attempted_Call__c = true;
update ca;
   
      }
   }
}

 

 

All Answers

ShwetaSShwetaS

Hi,

You will have to check if ParentID field is not null. This will assure you that the case which you are creating is a child.
Then fetch the "ParentID" value from the new case and then update the parent case using the "ParentId" value.


Shweta
Salesforce Developer Support

If my answer solved your question, please mark it solved so I can help as many community members as possible!

arnaud_carnaud_c

Thanks for your response. Unfortunately I do not know how to code an apex trigger for that. I have a minimal understanding of this.

 

can you please show me how the code should look like ?

 

thanks

TrueCloudTrueCloud

You are using Trigger.new[0]. This will only update the first Case record that was triggered. I am not sure if you want to do that. 

 

Here is an Example:

 

 

trigger UpdateParentCase on Case (after insert, after update){
Case relcase = Trigger.new;
Case ca = new Case();
for(Case c:[Select Id,  Attempted_Call__c from Case where
if (c != null) { // This will check if there is a Parent Case or associated or not.

Id = :relcase.Parent limit 1]){
ca = c;
ca.Attempted_Call__c = true;
update ca;
   
      }
   }
}

 

 

This was selected as the best answer
arnaud_carnaud_c

Hi again,

 

thanks for your response. I had to modify the code slightly after some trial and error and finally got it. here it is below. Thanks for your help on this.

 

 

trigger UpdateParentCase on Case (after insert, after update){
Case rel = Trigger.new[0];
Case ca = new Case();


for(Case c:[Select Id,  Attempted_Call__c from Case where
Id = :rel.ParentId limit 1]){

if (c != null) {
ca = c;
c.Attempted_Call__c = True;
update c;
               }

                              }
}
AB003AB003

Hi Can you help with the same trigger.

 

I want to update the status field on parent case when child case status is updated? Is it possible. Can you send me the sample for that?

 

Thanks

Abi