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
Chinna SfdcChinna Sfdc 

Need Trigger help

Hi Team,

I have a two fields in opportunity object those are "Help(Picklist)" and "Description(Text)". If i select "help" pick list value is Manager and entering in the "description" field  is Test,that time automatic task has been creating along with opportunity name based on below trigger.But my problem is whenever i removed the specific values then that time also task is creating with Null values.How to include that logic in my existing trigger.And also If i change the Description value in the task object it should change the same in Opty field also.Please help us.

Trigger UpdateTask on Opportunity(after insert,after update){    
    List<Opportunity> listOpp = Trigger.new;
    List<Task> listTask = new List<Task>();    
    /* New Task */
    if(Trigger.isInsert){
        for(Opportunity opp:listOpp){    
           if(opp.Help__c != null){
                Task t = new Task();
                t.OwnerId = opp.OwnerId;
                t.WhatId = opp.Id;
                t.Subject ='Help' + ': ' +opp.Help__c + ': '  + opp.Name; 
                t.ActivityDate = System.Today()+7;  
                t.Description = opp.Description__c;
                t.Status = 'In Progress';
                t.Priority = 'Normal';
                listTask.add(t);
            } 
        }
    }   
    // UPDATE the Same Task 
     if(Trigger.isUpdate){
        Set<Id> setOppId = new Set<Id>();
         Map<Id,Opportunity> oldOppMap = Trigger.oldmap;
        for(Opportunity opp:listOpp){
            if(Opp.Help__c != oldOppMap.get(opp.Id).Help__c || Opp.Description__c!= oldOppMap.get(opp.Id).Description__c){
            Task t = new Task();
            t.WhatId = opp.Id;   
            t.Subject = 'Help' + ': ' +opp.Help__c + ': '  + opp.Name; 
            t.Description = opp.Description__c;  
            t.ActivityDate = System.Today()+7;
            listTask.add(t);
         }
        }
    } 
        insert listTask;
}

Thanks in Advance
 
buggs sfdcbuggs sfdc
HI 

Try adding if condition in the following
Trigger UpdateTask on Opportunity(after insert,after update){    
    List<Opportunity> listOpp = Trigger.new;
    List<Task> listTask = new List<Task>();    
    /* New Task */
    if(Trigger.isInsert){
        for(Opportunity opp:listOpp){    
           if(opp.Help__c != null && opp.Help__c == 'Manager' ){
                Task t = new Task();
                t.OwnerId = opp.OwnerId;
                t.WhatId = opp.Id;
                t.Subject ='Help' + ': ' +opp.Help__c + ': '  + opp.Name; 
                t.ActivityDate = System.Today()+7;  
                t.Description = opp.Description__c;
                t.Status = 'In Progress';
                t.Priority = 'Normal';
                listTask.add(t);
            } 
        }
    }   
    // UPDATE the Same Task 
     if(Trigger.isUpdate){
        Set<Id> setOppId = new Set<Id>();
         Map<Id,Opportunity> oldOppMap = Trigger.oldmap;
        for(Opportunity opp:listOpp){
            if(Opp.Help__c != oldOppMap.get(opp.Id).Help__c || Opp.Description__c!= oldOppMap.get(opp.Id).Description__c && opp.Help__c == 'Manager'){
            Task t = new Task();
            t.WhatId = opp.Id;   
            t.Subject = 'Help' + ': ' +opp.Help__c + ': '  + opp.Name; 
            t.Description = opp.Description__c;  
            t.ActivityDate = System.Today()+7;
            listTask.add(t);
         }
        }
    } 
        insert listTask;
}


If it helps you please mark it as correct answer!

Thanks