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 

Insert Value on Task from linked Account

Hello Developers,

I have the following trigger which pulls data (Leadevent_Kategorie__c) from the lead / contact and writes it after creation/insert on the task (tk.Vertriebsaktion__c).
trigger UpdateLeadeventKategorieTask on Task (before insert) {
    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;
            }
        }
    } 
    
}
This works just perfect. But what would need in addition is the scenario where a task is neither linked to a lead 
if(t.WhoId != null){
              String s1 = t.WhoId;
               if(s1.left(3) == '00Q'){
nor to a contact
else if(s1.left(3) == '003'){
                       contactId.add(t.whoId);

but only to an account. So in fact 'WhoId' would be emtpy and 'WhatId' would begin with '001'. 

Where and how can I add this condition?

Greetings and Thanks in Advance!

Jan
 

Best Answer chosen by Janno Rip
SarvaniSarvani
Hello Jan,

Try below code.
trigger UpdateLeadeventKategorieTask2 on Task (before insert) {
    Set<Id> leadId = new Set<Id>();
    Set<Id> contactid = new Set<Id>();
   Set<Id> accountid = 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);
               }
                if ((s1.left(3)) == '003'){
                       contactId.add(t.whoId);
               }
            }
        if(t.WhoId == null && t.whatid != null){
                String s2 = t.WhatId;
                if(s2.left(3) == '001'){
                 accountId.add(t.WhatId);
                }
         }
   }
    
    
    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]);
    Map<Id,Account> accountMap = new Map<Id,Account>([Select Id, Leadevent_Kategorie__c From Account Where Id In : accountId]);
    
    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;
            }
            if(!contactMap.isEmpty() && contactMap.containsKey(tk.WhoId)){
                tk.Vertriebsaktion__c = contactMap.get(tk.WhoId).Leadevent_Kategorie__c;
                }
            if(!accountMap.isEmpty() && accountMap.containsKey(tk.WhatId)){
                tk.Vertriebsaktion__c = accountMap.get(tk.WhatId).Leadevent_Kategorie__c;
                }  
            }
        }
    }

Hope this helps, Mark as solved if it did.

Thanks,
Sarvani
 

All Answers

SarvaniSarvani
Hello Jan,

Try below code
trigger UpdateLeadeventKategorieTask on Task (before insert) {
    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);
               }
         }
// For tasks having only WhatId as accounts
		 else(t.WhoId == null && t.whatid!=NULL){
		 String IDvalue=t.Whatid;
		 if(Idvalue.left(3)=='001'){
		 /*Do something*/
		 }
		 }
    }

    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;
            }
        }
    } 
    
}

Hope this helps,

Thanks,
Sarvani
Janno RipJanno Rip
Hello Sarvani,

I tried your code and added my "Do something" to it, according to the structure it already had. I did the new code in "bold":

But I am getting: Error: compilation errorr: Missing ';' at '{' in row 17, column 51
trigger UpdateLeadeventKategorieTask2 on Task (before insert) {
    Set<Id> leadId = new Set<Id>();
    Set<Id> contactid = new Set<Id>();
    Set<Id> accountid = 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);
               }
         }

         else(t.WhoId == null && t.whatid != null){
                String s2 = t.WhatId;
                if(s2.left(3) == '001'){
                   accountId.add(t.WhatId);
                }
         }
    }

    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]);
    Map<Id,Account> accountMap = new Map<Id,Account>([Select Id, Leadevent_Kategorie__c From Account Where Id In : accountId]);
    
    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;
                }
            else (!accountMap.isEmpty() && accountMap.containsKey(tk.WhatId)){
                tk.Vertriebsaktion__c = accountMap.get(tk.WhatId).Leadevent_Kategorie__c;
                }  
            }
        }
    } 
    
}

 
SarvaniSarvani
Hello Jan,

Try below code.
trigger UpdateLeadeventKategorieTask2 on Task (before insert) {
    Set<Id> leadId = new Set<Id>();
    Set<Id> contactid = new Set<Id>();
   Set<Id> accountid = 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);
               }
                if ((s1.left(3)) == '003'){
                       contactId.add(t.whoId);
               }
            }
        if(t.WhoId == null && t.whatid != null){
                String s2 = t.WhatId;
                if(s2.left(3) == '001'){
                 accountId.add(t.WhatId);
                }
         }
   }
    
    
    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]);
    Map<Id,Account> accountMap = new Map<Id,Account>([Select Id, Leadevent_Kategorie__c From Account Where Id In : accountId]);
    
    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;
            }
            if(!contactMap.isEmpty() && contactMap.containsKey(tk.WhoId)){
                tk.Vertriebsaktion__c = contactMap.get(tk.WhoId).Leadevent_Kategorie__c;
                }
            if(!accountMap.isEmpty() && accountMap.containsKey(tk.WhatId)){
                tk.Vertriebsaktion__c = accountMap.get(tk.WhatId).Leadevent_Kategorie__c;
                }  
            }
        }
    }

Hope this helps, Mark as solved if it did.

Thanks,
Sarvani
 
This was selected as the best answer
Janno RipJanno Rip
Thanks Sarvani, that worked!