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
kishore cmxkishore cmx 

write a trigger automatically create task when iam creating case in case object?

Virendra ChouhanVirendra Chouhan
Use workflow instead of trigger with " Create Task " action 
Vijay NagarathinamVijay Nagarathinam
Hi Kishore,

You can create a task in two ways using Workflow and Apex trigger. Use the following code it will be working as you expected.
 
trigger autoCreateTask on Case (after insert, after update) 
{
List<Task> insertTask = new List<Task>();

for(Case newCase : Trigger.new)
{
Task newTask = new Task();
newTask.subject = 'Automated Task';
newTask.whatId = newCase.Id;
newTask.ownerId = newCase.OwnerId;
newTask.status = 'In progress';
newTask.Priority = 'Normal';
insertTask.add(newTask);
}
if(insertTask.size() > 0)
insert insertTask;
}

 
NagaNaga (Salesforce Developers) 
Hi Kishore,

Here is the sample code for trigger which will create a new Task when a new event is created on account/opportunity. 

trigger createnewTask on Event (after insert){ 

List<Task> tasks = new List<Task>(); 

List<Event> ev = Trigger.new; 

for (Event evv : ev) { 

Task tsk = new Task(Ownerid = evv.OwnerId, Subject=evv.Subject, whatid=evv.whatid);  //pass values to new task (use whoid=evv.whoid in case of contact)

tasks.add(tsk);  //add task to record



insert tasks; 


Can you try and replace "EVENT" with case?

Best Regards
Naga kiran