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
Code+1Code+1 

Trigger - Update Task field based on Case field

Hi Team,
 I have a custom field on task object.
 Whenever, there is a change in Case custom field, i would like to update the custom field in task.
 Please let me know, to achieve it through trigger ..
Best Answer chosen by Code+1
venkat-Dvenkat-D
And to get customfield, use below
Trigger.newmap.get(child.WhatId).customfield to get value of custom field on case

All Answers

venkat-Dvenkat-D
You can write trigger on case update. 

Query for tasks related to that case and update the value. 

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers.htm?search_text=triggers
Code+1Code+1
Hi Venky,
 Thanks for your response.
 I want to update the 'customfield' in task, when ever case is created / edited based on one custom field in case.
 Please check the below code and help me out.


trigger XXXX on Case (after insert, after update){
        List<Task> tskrecords = [Select Id, customfield, WhatId from Task where WhatId IN : Trigger.newMap.keySet()];
        for(Task child :tskrecords){
            if(trigger.isInsert && child.WhatId.customfield == '1234'){
                child.customfield = '0000';
            }
            else if(trigger.isUpdate && child.WhatId.customfield == '1234'){
                child.customfield = '0000';
            }
        }
        if(tskrecords.size() > 0)
            update tskrecords;
}
venkat-Dvenkat-D
The code looks good. if you want it in both update and insert you can simply out it into single if statement.
venkat-Dvenkat-D
And to get customfield, use below
Trigger.newmap.get(child.WhatId).customfield to get value of custom field on case
Code+1Code+1
child.WhatId.customfield is not working,
please let me know, how to traverse from task to case custom field to check for the condition.
 
venkat-Dvenkat-D
And to get customfield, use below
Trigger.newmap.get(child.WhatId).customfield to get value of custom field on case
This was selected as the best answer
Code+1Code+1
Thanks Venky, It worked...