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
Bryn JonesBryn Jones 

create a task based on a feild in a contact using apex

Hello,

We have a number of contacts that do not have tasks. Based on a field on the company of the contact we woudl like to create a task for the contact. woudl like to do it using apex so it runs without having to edit objects to get a workflow or process builder to run.

Thanks

VinayVinay (Salesforce Developers) 
Hi Jones,

Try below snippet.

This will invoke when Status field on Contact is 'Inactive' and creates task.
 
trigger AutoCreate on Contact (after  insert, after update)  {
    list<task> tasklist = new list<task>();    
    for(contact con : trigger.new){
	if (con.Status__c == 'Inactive') {	
	  Task tsk = new Task();
	  tsk.whoId=con.id;
	  tsk.Status = 'In Progress';
	  tsk.Subject='call';
	  tsk.Priority = 'High';
	  tsk.ownerid= con.ownerid;
	  tasklist.add(tsk);
    }
	insert tasklist;
	}
}

Hope above information was helpful.

Please mark as Best Answer so that it can help others in the future.

Thanks,
Vinay Kumar