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
cmaine3cmaine3 

Trigger Add Picklist Value to Task

Hello everyone-

This should be a quick slam dunk for any trigger masters out there and should help me to start learning myself.

 

Goal-

When a task is added to an Opportunity the lookup field Rep_Team__c on the opportunity record is checked. If the field is populated then it will take that value and convert it to the corresponding task record picklist value Rep_Tm__c.

 

This is 1:1 conversion or nothing. (meaning if the picklist value is not available then it will be left alone.)

 

I really wish I could give more information than this but I don't understand the syntax and am hoping that by seeing the working code I can learn to create my own.

 

Thanks for any help!!

 

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
SargeSarge

Hi,

 

   Please try the following code

 

 

trigger populatePicklist on Task(before insert, before update){
    
    
    String oppPrefix = Schema.SObjectType.Opportunity.getKeyPrefix();
    List<Id> oppIds = new List<Id>();
    
    for(Task t: Trigger.new){
        String relatedTo = (String)t.WhatId;
        String relatedToPrefix = relatedTo.subString(0,3);
        if(oppPrefix == relatedToPrefix){
           oppIds.add(t.WhatId);
        }
    }
    
    Map<Id,Opportunity> oppMap = new Map<Id,Opportunity>();
    if(!oppIds.isEmpty()){
        oppMap.putAll([Select Id, Name, Rep_Team__c, Rep_Team__r.Name from Opportunity where Id IN :oppIds]);
        
    
          for(Task t: trigger.new){
             String relatedTo = (String)t.WhatId;
             String relatedToPrefix = relatedTo.subString(0,3);
             if(oppPrefix == relatedToPrefix){
                if(oppMap.containsKey(t.WhatId)){
                   Opportunity opp = oppMap.get(t.WhatId);
                   if(opp.Rep_Team__c != null){
                      t.Rep_Tm__c = opp.Rep_Team__r.Name; //or some 1:1 conversion logic
                   }
                }
             }
          }
    }
}

 Hope this helps

 

All Answers

SargeSarge

Hi,

 

   Please try the following code

 

 

trigger populatePicklist on Task(before insert, before update){
    
    
    String oppPrefix = Schema.SObjectType.Opportunity.getKeyPrefix();
    List<Id> oppIds = new List<Id>();
    
    for(Task t: Trigger.new){
        String relatedTo = (String)t.WhatId;
        String relatedToPrefix = relatedTo.subString(0,3);
        if(oppPrefix == relatedToPrefix){
           oppIds.add(t.WhatId);
        }
    }
    
    Map<Id,Opportunity> oppMap = new Map<Id,Opportunity>();
    if(!oppIds.isEmpty()){
        oppMap.putAll([Select Id, Name, Rep_Team__c, Rep_Team__r.Name from Opportunity where Id IN :oppIds]);
        
    
          for(Task t: trigger.new){
             String relatedTo = (String)t.WhatId;
             String relatedToPrefix = relatedTo.subString(0,3);
             if(oppPrefix == relatedToPrefix){
                if(oppMap.containsKey(t.WhatId)){
                   Opportunity opp = oppMap.get(t.WhatId);
                   if(opp.Rep_Team__c != null){
                      t.Rep_Tm__c = opp.Rep_Team__r.Name; //or some 1:1 conversion logic
                   }
                }
             }
          }
    }
}

 Hope this helps

 

This was selected as the best answer
cmaine3cmaine3

Thanks a lot! It works and now I can start to see where the logic is for some of the values. Thanks again.