+ Start a Discussion
kishore cmxkishore cmx 

How to write a trigger when i creating a case in case object at the same time one task will be created?

Best Answer chosen by kishore cmx
Vijay NagarathinamVijay Nagarathinam
Hi,

You can create a task in two ways using Workflow and Apex trigger. Use the following code it will be work.
 
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;
}


 
 

All Answers

KaranrajKaranraj
Kishore - You don't need to write trigger code for this scenario, you can achieve this using simple Workflow rule.
  • Create a workflow rule in the case object and set the evaluvation criteria as 'Every time created'
  • Set your rule criteria (Condition when the record should enter) and click Save and Next
  • Then in the workflow action choose New task.
To know more about Workflow rule, check this trailhead module - https://developer.salesforce.com/trailhead/force_com_admin_intermediate/business_process_automation/workflow

Thanks,
Karan
Vijay NagarathinamVijay Nagarathinam
Hi,

You can create a task in two ways using Workflow and Apex trigger. Use the following code it will be work.
 
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;
}


 
 
This was selected as the best answer
kishore cmxkishore cmx
Hi vijay,
plz write a trigger to count the contacts in contact object , that will disply on the number of contacts field on cotact object?
 
Vijay NagarathinamVijay Nagarathinam
Sure kishore, I already added my comments in your question. Please check
kishore cmxkishore cmx
Hi vijay ,Present I am learning the saleforce. ur having any realtime interview qestions plz send me .that is very helpful to me