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
NJH-GDNJH-GD 

Updating custom field on parent case when child case is created

Hello - 

I am trying to figure out the best way to update a custom field on the parent level case everytime an Action Item (child case) is created with the Action Item's (child case) Contact Name.  I tried to use Process Builder to do this, but to no avail.  Any thoughts?  Would a trigger do this?  If so, what would it look like?
Thiago NegherbonThiago Negherbon
What I would try to do is:
- Create a Trigger that triggers after the Action Item is inserted
- Within that Trigger I would get the related parent using SOQL
- Change the value of the parent object's custom field (remember the __c rule) with the Contact Name
- Call a DML command to update the parent record

Hope it helps!
priti jaiswal 17priti jaiswal 17
Not possible via process builder. 
You can achieve this via trigger. Just follw the steps descrribed by Thiago.
Let me know if you need any help in this.
NJH-GDNJH-GD
Thanks for the help guys!  As a SFDC newbie, I am not sure what this code would look like.  Is there some example you could point me to?
priti jaiswal 17priti jaiswal 17
try this code.
I have a custom field "Contact" on "Case", and updating this field on Parent Case when a child case with "Contact" is created.
Please replacae this code with respective fields in your org.


//contact__c - your custom field on Case
//Parent_Case_c - lookup field on case, that links parent case to child case

Trigger updateParentCase on Case(after insert)
{   
    set<Id> setOfParentCaseIds = new Set<Id>();
    Map<Id, Id> mapOfParnetCaseIdAndContact  = new Map<Id, Id>();
    
    for(Case objCs : Trigger.new)
    {
        System.debug('contact--'+objcs.Contact__c +'parent case--'+objcs.Parent_Case__c);
        if(objcs.Contact__c != null && objcs.Parent_Case__c != null)
        {
            setOfParentCaseIds.add(objcs.Parent_Case__c);
            //creating a map of parent case Id and associated contact
            mapOfParnetCaseIdAndContact.put(objcs.Parent_Case__c, objcs.Contact__c);
        }
    }
    System.debug('setofcaseids--'+setOfParentCaseIds +'map--'+mapOfParnetCaseIdAndContact);
    if(setOfParentCaseIds != null && !setOfParentCaseIds.isEmpty())
    {
        List<Case> lstOfParentCases = new List<Case>();
        //get list of parent cases
        lstOfParentCases = [select Contact__c from Case where id in : setOfParentCaseIds];
        System.debug('lstOfParentCases --'+lstOfParentCases );
        if(lstOfParentCases != null && !lstOfParentCases.isEmpty())
        {
            for(Case objcs : lstOfParentCases)
            {
                System.debug('parent case--'+mapOfParnetCaseIdAndContact.get(objcs.id));
                if(mapOfParnetCaseIdAndContact != null && mapOfParnetCaseIdAndContact.containsKey(objcs.id))
                    objcs.Contact__c = mapOfParnetCaseIdAndContact.get(objcs.id);               
            }
            //update parent case
            update lstOfParentCases;            
        }       
    }
    
}

Let me know if it works for your.