+ Start a Discussion
Abhinav AdminAbhinav Admin 

Need to know how to change the status of a Task

Hi i am working on an object there i am creating two tasks by using workflows once when a new record is inserted and once we assign some employee to that record. now i need to know how i can change the status of a task to complete. am i achieve through Workflow or need to Write any trigger. if Possible please guide me with the code.

Thanks & Regards,
Abhinav
KaranrajKaranraj
Hi Abhinav - You can able to do that using process builder, no code is required. Create a Process builder for the object and set the criteria as Account.OwnerId and isChanged and True and in the action update records select the object as Task and update the field task status as completed. To know more about process builder check this link (https://developer.salesforce.com/trailhead/force_com_admin_intermediate/business_process_automation/process_builder

Thanks,
Karanraj
Abhinav AdminAbhinav Admin
Thanks for your Suggestion Karanraj, But

I donot want to use any process builders. I would like to achieve it through Workflow or Trigger is  it Possible.

Thanks and Regards,
Abhinav
KaranrajKaranraj
You can't update child records using workflow rule, so only option is you have write the trigger code. Below is the trigger code writen on Account object which will mark the status of task to 'completed' for the account when the owner is changed it will update the respective task status completed. Modify the code based on your condition and objects
 
trigger updateTask on Account(after update){
	List<Account> listAccount = new List<account>();
	List<Task> listTask = new List<Task>();
	for(Account acc: Trigger.new){
		if(acc.ownerId != Trigger.oldMap.get(acc.Id).OwnerId){
			listAccount.add(acc);
		}
	}
	for(Task updateTask: [Select Id,Status from Task where WhatId IN:listAccount]){
		updateTask.status ='Completed';
		listTask.add(updateTask);
	}
	if(listTask.size() >0)
		update listTask;
}



 
Amit Chaudhary 8Amit Chaudhary 8

Hi Abhinav Admin,

You cant update the child record by workflow because one parent record can multiple child record. You can use "Cross-Object Field Updates" to update parent record. 

Cross-object field updates work for custom-to-custom master-detail relationships, custom-to-standard master-detail relationships, and a few standard-to-standard master-detail relationships. For example, in a custom recruiting application, create a workflow rule that sets the status of an application (the master object) to “Closed” when a candidate (the detail object) accepts the job

If you want to update child record base on parent record then you need to create trigger.

Thanks,
Amit Chaudhary