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
SriniSrini 

How to create a task when opportuinty field is updated ?

Hi Team,

We have one Requirement Hear we have some fields in the opportunity those are  Status,Help,Description. Here we want to entering some details in the Description field for that time automatically Task should be create with Subject name TestDescription.

Through trigger is it possible? if possible please help us along wih code.

Thanks in Advance


 
Best Answer chosen by Srini
SandhyaSandhya (Salesforce Developers) 
Hi Srini,

Please find updated code.
 
trigger createTask on Opportunity (After update){ 
List<Task> tasks = new List<Task>(); 
List<Opportunity> Opps = Trigger.new;
 for (Opportunity Opp : Opps) 
{ 
    if(Trigger.oldMap.get(opp.id).Description!=opp.Description)
    {
    Task tsk = new Task();
    tsk.WhatId      = opp.Id;
    tsk.Subject     = 'Follow up test task';
    tasks.add(tsk);
 } 
insert tasks; 
}
}

Please let us know if you need further help.
 

Please accept my solution as Best Answer if my answer was helpful. It will make it available for other as the proper solution. If you felt I went above and beyond, You can give me kudos.

 

Thanks and Regards

Sandhya

All Answers

VineetKumarVineetKumar
Can't you use workflow to create the new task?
Though this can be done using a trigger, but why not utilize the point and click feature of salesforce to meet your requirement.
This will help reduce your code maintenance and your writing of test classes while deploying the code to production.

Also, you can refer process builder as well.
https://developer.salesforce.com/trailhead/en/project/quickstart-process-builder
SandhyaSandhya (Salesforce Developers) 

Hi Srini ,
 You can write a trigger to create a task when opportunity field is updated.
 
Below is the example code for your requirement you can make changes as per your need.
 
Before that,please refer below link to know about triggers.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers.htm
 
and also trailhead module to get a better idea of a trigger.
 
https://developer.salesforce.com/trailhead/en/apex_triggers/apex_triggers_intro
 
trigger ClosedOpportunityTrigger on Opportunity (after insert, before update) {

//Create a list to hold all opportunities for which tasks need to be created
List<Opportunity> oppList = new List<Opportunity>();

for(Opportunity opp: Trigger.new) {

    if(Trigger.isInsert)    {
        if(opp.StageName=='Closed Won') {
            oppList.add(opp);
        }
    }

       //Make sure trigger only works on updated Opportunities 
       where old value != new value
       else {

        Opportunity oldOpp  = Trigger.oldMap.get(opp.Id);
        Boolean oldOppIsWon = oldOpp.StageName.equals('Closed Won');
        Boolean newOppIsWon = opp.StageName.equals('Closed Won');

        if(!oldOppIsWon && newOppIsWon) {

            oppList.add(opp);
        }
    }

  }

List<Task> taskList = new List<Task>();
for(Opportunity opp :oppList) {

    Task t        = new Task();
    t.WhatId      = opp.Id;
    t.Subject     = 'Follow up test task';
    t.Priority    = 'Normal';
    t.Status      = 'Not Started';
    taskList.add(t);
}

insert taskList;

}
Please accept my solution as BestAnswer if my answer was helpful.It will make it available for other as the proper solution.If you felt I went above and beyond, u can give me kudos.

Thanks and Regards
sandhya
 
SriniSrini
Hi Sandya,

Thanks for your solution.My problem is I want to entering something in Description Field it will create automatic Task with the subject "Follow up test task".The above code is the best solution in that code based on stage name they were updated.But my requiremenr is: For example In Description Field am Entering "Test task" so for that time task should be created with the subject Test Task.

Description Field is the Oppty field: 
Based on this field create a brand new task.


Can you please Help us for complete Trigger. because am the new guy for this. Will appricate you.

Once again thanks for your concern!....


Thanks in Advance.
SandhyaSandhya (Salesforce Developers) 
Hi Srini,

Below code works.
trigger createTask on Opportunity (before insert, before update){ 
List<Task> tasks = new List<Task>(); 
List<Opportunity> Opps = Trigger.new;
 for (Opportunity Opp : Opps) 
{ 
Task tsk = new Task();
    t.WhatId      = opp.Id;
    t.Subject     = 'Follow up test task';
    taskList.add(t);
 } 
insert tasks; 
}
Hope this helps you.

Thanks and Regards
sandhya

 
SriniSrini
Hi Sandya,

That's awsome....Working fine.

But hear one problem after creating the task.We need to assign that particular task to one of the Group member. after completion the task, completion mail should be getting opportunity Manager like.

Hello XXX, 
Your Task has been Completed. Here are the details -
Subject : Test
Status : Completed
Priority : Low

Can you please help us that would be great for us.

Thanks in Advance.


 
SriniSrini
Hi Sandya,

Above trigger is working but i havn't updated any field,Automatically created a task.But i want to updated one field called as Description after that a brand new task should be create.Can you please tell me where i need to updated the field in the trigger.

Thanks 
SandhyaSandhya (Salesforce Developers) 
Hi Srini,

Please find updated code.
 
trigger createTask on Opportunity (After update){ 
List<Task> tasks = new List<Task>(); 
List<Opportunity> Opps = Trigger.new;
 for (Opportunity Opp : Opps) 
{ 
    if(Trigger.oldMap.get(opp.id).Description!=opp.Description)
    {
    Task tsk = new Task();
    tsk.WhatId      = opp.Id;
    tsk.Subject     = 'Follow up test task';
    tasks.add(tsk);
 } 
insert tasks; 
}
}

Please let us know if you need further help.
 

Please accept my solution as Best Answer if my answer was helpful. It will make it available for other as the proper solution. If you felt I went above and beyond, You can give me kudos.

 

Thanks and Regards

Sandhya

This was selected as the best answer
SriniSrini
Hi Sandya,

It was super.Thank you so much.

The Above code is working fine but we are using before insert and before update.But i want to use proper subject name.
For example: If i give the name in the description field like "IM Test Description" the same name  should be  populate in the task subject name along with "Follow up Task".  Finally  output should be like this Subject name in the Task: "IM Test Description Follow up Task".how we can acheive this.

Can you please help us.

Thanks in Advance
SandhyaSandhya (Salesforce Developers) 
Hi Srini,

Please replace tsk.Subject = 'Follow-up test task'; 
 line with below code  
 
tsk.Subject     = opp.Description;

Please let us know if this helps you.

Thanks and Regards
sandhya
 
SriniSrini
Hi Sandya,

We have added the above line that's awsome....Working fine.  Thank you so much for your help!...

I have given the best ansawer the one which you have provided the solution.

please help below issue also.

But hear one problem after creating the task.We need to assign that particular task to one of the Group member. after completion the task, completion mail should be getting opportunity Manager like.

Hello XXX, 
Your Task has been Completed. Here are the details -
Subject : Test
Status : Completed
Priority : Low

Can you please help us that would be great for us.

Thanks in Advance.
SandhyaSandhya (Salesforce Developers) 
Hi srini,

I will work on that and let you know.

Thanks and Regards
sandhya
SriniSrini
Hi @Sandhya,

I hope you for the solutin the ablove one..Please help us.

Thanks
SandhyaSandhya (Salesforce Developers) 
Hi Srini,

Did you work on that and stuck somewhere, then please post your code I will help you.

Meanwhile i will also try to work on that.

Thanks and Regards
sandhya
SriniSrini
Hi Sandya,

After creating automatic task along with proper comments,we are not able to find Publicgroups in AssignTo Field in the Task page layout.don't know how to acheive this.Is there any other way to sending completion mails to Opty owners and their managers.Please help us.

Thanks