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
Anthony MossAnthony Moss 

Update Task status based on field change in parent record

New to Apex. Trying to create a trigger that would update the status field of a task based on a field update in the parent (specifically opportunity) record? Can anyone help me out? Thanks in advance!
 
KaranrajKaranraj
You don't need to trigger for this, you can update the task status based on change in parent record using Process builder

Follow the below steps,
1. Goto Setup->Create->Workflow&Approval->Process Builder
2. Click New and enter the name for the process
3. Click 'Add Object' and select Object as "Opportunity" and select "when record is created or edited"
4. Set the criteria in which you want to update the task object
5. Click "Add action" and select "Update Record" as Action Type
6. Choose "Tasks" in the Object
7. Select the field you want to update in the task and Save your action
8. Click "Activate" 

Hope this reolves your problem without writting a trigger
Tejpal KumawatTejpal Kumawat
Hello Anthony,

Use this code :

trigger OpportunityTest on Opportunity (after update) {
    set<string> setOpp = new set<string>();
    for(Opportunity opp : Trigger.New){
        //put if condition which check on which field update
        setOpp.add(opp.Id);
    }
    
    list<Task> listTask = new list<Task>();
    for(Task t : [Select Status from Task where WhatId IN : setOpp]){
        t.Status = 'Not Started';
        listTask.add(t);
    }
    
    if(listTask.size() > 0)
        update listTask;
}

If this answers your question then hit Like and mark it as solution!
Amol ChAmol Ch
@KaranRaj, After updating lead status, i have to update status of all task , related to that lead. I have  a probelm in getting 'value' of 'related record
id' of all task related  to that lead reocrd. Please help
User-added image