+ Start a Discussion
Chinna SfdcChinna Sfdc 

If update the field needs to be create new task

Hi Team,

Whenever update the Opty fields create a new brand task along with opportunity name. for that we have written a trigger working as expected. But once create a task if we update the description field it should creat a new task not overriding the exting task. 

Every time Step(OptyField) or Description(OptyField) changes, New task should be created

Example: Step = Legal,Description = test 

If I again select Step = Legal and Description = test1234  new task should be created. Currently its updating test to test1234.

Please Find my below trigger and let us know where we made the mistakes.

Trigger Task on Opportunity(after insert,after update){
Set<Id> opIds = new Set<Id>();
List<Task> taskList = new List<Task>();
List<Opportunity> Opps = Trigger.new;
List<Opportunity> taskOps = [Select Id,Steps__c,Description__c from Opportunity where Id in :opIds];
                            
if(Trigger.isInsert){
   for (Opportunity Opp: Opps){
        Task t = new Task();
        t.WhatId = opp.Id;
        t.Subject = 'Help' + ': ' +opp.Steps__c + ': '  + opp.Name; 
        t.Description = opp.Description__c;  
         t.ActivityDate = System.Today()+7;     
        taskList.add(t);
    }
}
//Update 
if(Trigger.isUpdate){
    Map<Id,Opportunity> oldOppMap = Trigger.oldmap;
     for (Opportunity Opp: Opps){
         // description got changed
         if(Opp.Steps__c != oldOppMap.get(opp.Id).Steps__c ){
            Task t = new Task();
            t.WhatId = opp.Id;       
             t.Subject = 'Help' + ': ' +opp.Steps__c + ': '  + opp.Name; 
             t.Description = opp.Description__c;  
             t.ActivityDate = System.Today()+7;
        
            taskList.add(t);
         }
    }
   }
insert taskList;
}

Thanks in Advance
RAM AnisettiRAM Anisetti
Hi,
Modifiy your code like this
if(Opp.Steps__c != oldOppMap.get(opp.Id).Steps__c || Opp.Description__c!= oldOppMap.get(opp.Id).Description__c){

//add your task create code

}
sandeep reddy 37sandeep reddy 37
what the error in that
ok I understand 
so you did not done the dml operation after insert thats why you got error
Chinna SfdcChinna Sfdc
Hi RAM,

 Logic working as expected which you have mentioned above.We have done  "Creating new task" and "Update the same task" in opty so far.But i have one more problem.if i update  description (Comments) field  in the task object it should be change in the Opty Description__c field also..

Can you please help me on this.

Thanks

 
Swaraj Behera 7Swaraj Behera 7
Hi Chinna,
I have updated the code it is working fine in my org.Please use code.
Trigger Task on Opportunity(after insert,after update){
Set<Id> opIds = new Set<Id>();
List<Task> taskList = new List<Task>();
List<Opportunity> Opps = Trigger.new;
List<Opportunity> taskOps = [Select Id,Steps__c,Description__c from Opportunity where Id in :opIds];
                            
if(Trigger.isInsert){
   for (Opportunity Opp: Opps){
        Task t = new Task();
        t.WhatId = opp.Id;
        t.Subject = 'Help' + ': ' +opp.Steps__c + ': '  + opp.Name; 
        t.Description = opp.Description__c;  
         t.ActivityDate = System.Today()+7;     
        taskList.add(t);
    }
}
//Update 
if(Trigger.isUpdate){
    Map<Id,Opportunity> oldOppMap = Trigger.oldmap;
     for (Opportunity Opp: Opps){
         // description got changed
         if(Opp.Steps__c != oldOppMap.get(opp.Id).Steps__c || Opp.Description__c!= oldOppMap.get(opp.Id).Description__c){
            Task t = new Task();
            t.WhatId = opp.Id;       
             t.Subject = 'Help' + ': ' +opp.Steps__c + ': '  + opp.Name; 
             t.Description = opp.Description__c;  
             t.ActivityDate = System.Today()+7;
        
            taskList.add(t);
         }
    }
   }
insert taskList;
}

Thanks,
Swaraj Behera
Chinna SfdcChinna Sfdc
Hi Swaraj Behera,

Thanks for your help..

The above code is working fine to me also.The problem is If we  want to Update the description (Comments) field on task object it should be reflect on the opportinity field(Description) as well.
Let's Say :

description (Optyfield) :Test
Comments(Task obj) : Test

If am Changing the field value on the Task object  (Comments(Task obj) : Test12),Same should reflect on the Opportunity description field as well(description (Optyfield) :Test12).

Please help me on this.

Thanks