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
Janno RipJanno Rip 

Trigger: Pull data from Lead / Contact to newly created task

Hello Developers,

I have a custom field "Leadevent_Kategorie__c" on lead and contact. If this field is not blank, I want newly created tasks on these records to populate their field "Vertriebsaktion__c" with the value from "Leadevent_Kategorie__c".

I have the following trigger for leads:
trigger UpdateLeadeventKategorieLead on Task (before update) {
    Set<Id> leadId = new Set<Id>();
    for(Task t : Trigger.new){
        if(t.WhoId != null){
              String s1 = t.WhoId;
               if(s1.left(3) == '00Q'){
                        leadId.add(t.whoId);
               }
         }
    }

    Lead lead = [Select Id, Leadevent_Kategorie__c From Lead Where Id In : leadId limit 1];
    for(Task tk: Trigger.New){
           tk.Vertriebsaktion__c = lead.Leadevent_Kategorie__c;
    } 
    
}

and this one for contacts:
trigger UpdateLeadeventKategorieKontakt on Task (before update) {
    Set<Id> contactid = new Set<Id>();
    for(Task t : Trigger.new){
        if(t.WhoId != null){
              String s1 = t.WhoId;
               if(s1.left(3) == '003'){
                       contactId.add(t.whoId);
               }
         }
    }

    Contact contact = [Select Id, Leadevent_Kategorie__c From Contact Where Id In : contactId limit 1];
    for(Task tk: Trigger.New){
           tk.Vertriebsaktion__c = contact.Leadevent_Kategorie__c;
    } 
    
}
I always get the following error message: "System.QueryException: List has no rows for assignment to SObject Trigger"

Even though the value gets populated. I would also need to check if "Vertriebsaktion__c" on the task object is actually blank. If not - do not populated.




 
Best Answer chosen by Janno Rip
Maharajan CMaharajan C
Hi Janno,

Try to merge your triggers in to single as like below:

trigger UpdateLeadeventKategorieLead on Task (before update) {
    Set<Id> leadId = new Set<Id>();
    Set<Id> contactid = new Set<Id>();
    
    for(Task t : Trigger.new){
        if(t.WhoId != null){
              String s1 = t.WhoId;
               if(s1.left(3) == '00Q'){
                        leadId.add(t.whoId);
               }
               else if(s1.left(3) == '003'){
                       contactId.add(t.whoId);
               }
         }
    }

    Map<Id,Lead> leadMap = new Map<Id,Lead>([Select Id, Leadevent_Kategorie__c From Lead Where Id In : leadId]);
    Map<Id,Contact> contactMap = new Map<Id,Contact>([Select Id, Leadevent_Kategorie__c From Contact Where Id In : contactId]);
    
    for(Task tk: Trigger.New){
        if(tk.Vertriebsaktion__c == null)
        {
            if(!leadMap.isEmpty() && leadMap.containsKey(tk.WhoId)){
                tk.Vertriebsaktion__c = leadMap.get(tk.WhoId).Leadevent_Kategorie__c;
            }
            else if(!contactMap.isEmpty() && contactMap.containsKey(tk.WhoId)){
                tk.Vertriebsaktion__c = contactMap.get(tk.WhoId).Leadevent_Kategorie__c;
            }
        }
    } 
    
}


Thanks,
Maharajan.C

All Answers

Naren9Naren9
Hi Janno,
I tried the above code on Lead, it is working fine. I didn't faced any error. 

Thanks,
Naren
Maharajan CMaharajan C
Hi Janno,

Try to merge your triggers in to single as like below:

trigger UpdateLeadeventKategorieLead on Task (before update) {
    Set<Id> leadId = new Set<Id>();
    Set<Id> contactid = new Set<Id>();
    
    for(Task t : Trigger.new){
        if(t.WhoId != null){
              String s1 = t.WhoId;
               if(s1.left(3) == '00Q'){
                        leadId.add(t.whoId);
               }
               else if(s1.left(3) == '003'){
                       contactId.add(t.whoId);
               }
         }
    }

    Map<Id,Lead> leadMap = new Map<Id,Lead>([Select Id, Leadevent_Kategorie__c From Lead Where Id In : leadId]);
    Map<Id,Contact> contactMap = new Map<Id,Contact>([Select Id, Leadevent_Kategorie__c From Contact Where Id In : contactId]);
    
    for(Task tk: Trigger.New){
        if(tk.Vertriebsaktion__c == null)
        {
            if(!leadMap.isEmpty() && leadMap.containsKey(tk.WhoId)){
                tk.Vertriebsaktion__c = leadMap.get(tk.WhoId).Leadevent_Kategorie__c;
            }
            else if(!contactMap.isEmpty() && contactMap.containsKey(tk.WhoId)){
                tk.Vertriebsaktion__c = contactMap.get(tk.WhoId).Leadevent_Kategorie__c;
            }
        }
    } 
    
}


Thanks,
Maharajan.C
This was selected as the best answer
Janno RipJanno Rip

Hello Maharajan C,

Thanks. That seems to do the trick. Now reading your code I realized I missed the scenario where only an account (whatid , 001) is connected to a task. Any chance we can catch this too?

And it would be really nice to check whether tk.Vertriebsaktion__c is empty because I do not want to overwrite data.

Thanks!

Janno RipJanno Rip

Me again Maharajan C,

forget the last part "And it would be really nice to check whether tk.Vertriebsaktion__c is empty because I do not want to overwrite data." - I just reviewed the code again and found it.

Thanks!

Maharajan CMaharajan C
Vertriebsaktion__c is Lookup or String or what data type?
Janno RipJanno Rip
String.
Maharajan CMaharajan C
trigger UpdateLeadeventKategorieLead on Task (before update) {
    Set<Id> leadId = new Set<Id>();
    Set<Id> contactid = new Set<Id>();
    
    for(Task t : Trigger.new){
        if(t.WhoId != null && String.isEmpty(t.Vertriebsaktion__c)){
              String s1 = t.WhoId;
               if(s1.left(3) == '00Q'){
                        leadId.add(t.whoId);
               }
               else if(s1.left(3) == '003'){
                       contactId.add(t.whoId);
               }
         }
    }

    Map<Id,Lead> leadMap = new Map<Id,Lead>([Select Id, Leadevent_Kategorie__c From Lead Where Id In : leadId]);
    Map<Id,Contact> contactMap = new Map<Id,Contact>([Select Id, Leadevent_Kategorie__c From Contact Where Id In : contactId]);
    
    for(Task tk: Trigger.New){
        if(String.isEmpty(tk.Vertriebsaktion__c))
        {
            if(!leadMap.isEmpty() && leadMap.containsKey(tk.WhoId)){
                tk.Vertriebsaktion__c = leadMap.get(tk.WhoId).Leadevent_Kategorie__c;
            }
            else if(!contactMap.isEmpty() && contactMap.containsKey(tk.WhoId)){
                tk.Vertriebsaktion__c = contactMap.get(tk.WhoId).Leadevent_Kategorie__c;
            }
        }
    } 
    
}


Sorry Jan for the late reply am in busy with some other works
 
Janno RipJanno Rip
No problem at all. I am very grateful for your answer!