0

We have a requirement:
We have fields in the opportunity called as MIDescription Whenever we entered in the description field it should create a new brand task in the Open Activities related list. For that we have created a one trigger in opportunity mentioned below. Trigger is working but we haven't updated anything in the Description field but it has automatically creating a new task.
Can you please help us the trigger where i made the mistakes.

Trigger Description on Opportunity(before insert, before update) {
    Set < Id > opIds = new Set < Id > ();
    List < Task > taskList = new List < Task > ();
    List < Opportunity > Opps = Trigger.new;
    //List<Opportunity> taskOps = [Select Id,Help__c,Description__c from Opportunity where Id in :opIds];
    for (Opportunity Opp: Opps) {
        Task t = new Task();
        t.WhatId = opp.Id;
        t.Subject = 'Task';
        taskList.add(t);
    }
    insert taskList;
}

Thanks in advance.

2

It is creating every time you are updating opportunity. You need to put some check in your code to verify if the MIDescription is updated.

You can use trigger.oldMap to verify the changes in the value of MIDescription and then create a task. Use following code and let me know if it works.

Trigger Description on Opportunity(before update) {
    Set < Id > opIds = new Set < Id > ();
    List < Task > taskList = new List < Task > ();
    List < Opportunity > Opps = Trigger.new;

    for (Opportunity Opp: Opps) {
    if(opp.Description__c != Trigger.oldMap.get(opp.id).MI_Description__c){
        Task t = new Task();
        t.WhatId = opp.Id;
        t.Subject = 'Task';
        taskList.add(t);
    }
    }
    insert taskList;
}
  • Hi Prabhat Kumar, That's working fine.Thank you so much 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. – Devi Jul 11 '16 at 12:17
  • You can create a work flow and sent the email whenever MIDescription is updated. Follow this link to send email by workflow. shellblack.com/administration/actions – MIX DML Jul 11 '16 at 12:21
  • Hi Prabhat,Can you please help us how we can approach the issue.Hear we have One field Description. whenever we entered something in the description field it will creating a task with the subject name.For that trigger is working fine as mentioned above.In the task details we need to assign the same task ,means the one created along with description updated one task has been created,the same task we need to assign to some other user.After user can complete the task the completion mail should be sending to opportunity manager level.In workflow how we can approach.please help us. Thanks – Devi Jul 11 '16 at 12:35
  • It seems a different question, you can ask another question for more clarification. – MIX DML Jul 11 '16 at 12:50
0
Trigger.isInsert trigger context is used to check whether Trigger was fired due to Insert operation, in that case we want to create Task. 
Trigger.isUpdate trigger context is used during Update operation, get Old map from Trigger context check if Description(or any other field) got changed, then create Task

trigger Opportunity_Create_Task on Opportunity (before insert, before Update) {
Set < Id > opIds = new Set < Id > ();
List < Task > taskList = new List < Task > ();
List < Opportunity > Opps = Trigger.new;
List<Opportunity> taskOps = [Select Id,Description from Opportunity where Id in :opIds];
//Insert Opertaion
if(Trigger.isInsert){
   for (Opportunity Opp: Opps) {
        Task t = new Task();
        t.WhatId = opp.Id;
        t.Subject = 'QMI Task';
        taskList.add(t);
    }
}
//Update Operation
if(Trigger.isUpdate){
    Map<Id,Opportunity> oldOppMap = Trigger.oldmap;
     for (Opportunity Opp: Opps) {
         //Check If description got changed
         if(Opp.description != oldOppMap.get(opp.Id).description){
            Task t = new Task();
            t.WhatId = opp.Id;
            t.Subject = 'QMI Task';
            taskList.add(t);
         }

    }
}

insert taskList;

}

  • 2
    Please add some descriptions. Code dumps are not really an answer per se, and not very useful. – Adrian Larson Jul 11 '16 at 13:06
  • Trigger.isInsert trigger context is used to check whether Trigger was fired due to Insert operation, in that case we want to create Task. Trigger.isUpdate trigger context is used during Update operation, get Old map from Trigger context check if Description(or any other field) got change, then create Task – Pranav Chikodi Jul 11 '16 at 13:10
  • 2
    Please edit your post with any new information. Comments are not the appropriate way to add it. – Adrian Larson Jul 11 '16 at 13:11
  • Hi chikodi pranav, Thank you very much Your code is 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 – Devi Jul 11 '16 at 13:34
  • Hi Pranav , for the above requirement is there any possible to add the logic from above trigger? If possible please provide us the logic. Thanks – Devi Jul 12 '16 at 11:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.