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
Bob BaconBob Bacon 

Trigger that references a lead to copy a field to a task

Martijn Schwarzer kindly wrote a trigger for me that was an improvement over the one I had but I can't get it to save and Martijn must be on holiday...

Here is the trigger.  Can anyone figure out why I get this error?  "Compile Error: unexpected token: '{' at line 17 column 40"

trigger UpdateAccountType on Task (before insert , before update) {
    Set<Id> leadId = new Set<Id>();
    for(Task t : Trigger.new){
        if(t.WhoId != null && t.WhoId.left(3) == '00Q'){
            leadId.add(t.whoId);
        }
    }

    //Query all leads from trigger.new (I removed the Limit 1 --> this will only retrieve the first lead)
    List<Lead> leads = [Select Id, Type__c From Lead Where Id In : leadId];

    //Create map for easy retrieval of records
    Map<Id, Lead> leadMap = new Map<Id, Lead>(leads);

    for(Task tk: Trigger.New){
        //Check if lead is available in Map
        if(leadMap.containsKey(tk.WhoId){
            Lead lead = leadMap.get(tk.WhoId);  //Get lead from Map
            tk.Account_Type__c = lead.Type__c;
        }
    }
}

Thanks in advance.

Bob Bacon
@Karanraj@Karanraj
There is one close brace is missing in the if condition below the //check if lead available in Map field. Try the updated code below
trigger UpdateAccountType on Task (before insert , before update) {
    Set<Id> leadId = new Set<Id>();
    for(Task t : Trigger.new){
        if(t.WhoId != null && t.WhoId.left(3) == '00Q'){
            leadId.add(t.whoId);
        }
    }

    //Query all leads from trigger.new (I removed the Limit 1 --> this will only retrieve the first lead)
    List<Lead> leads = [Select Id, Type__c From Lead Where Id In : leadId];

    //Create map for easy retrieval of records
    Map<Id, Lead> leadMap = new Map<Id, Lead>(leads);

    for(Task tk: Trigger.New){
        //Check if lead is available in Map
        if(leadMap.containsKey(tk.WhoId)){
            Lead lead = leadMap.get(tk.WhoId);  //Get lead from Map
            tk.Account_Type__c = lead.Type__c;
        }
    }
}



 
Bob BaconBob Bacon
Thanks S.Karanraj, I still can't save it due to this error: Compile Error: Method does not exist or incorrect signature: [Id].left(Integer) at line 4 column 31
venkat-Dvenkat-D
Bob,
use t.WhoId.getSobject.Type == ObjectName.sobjettype to compare to vaoid hardcoding.
Bob BaconBob Bacon
Venky, is "t.WhoId.getSobject.Type == ObjectName.sobjettype" an insertion of a row or a substitution for existing code?  As you can tell, I'm a novice.

Thanks!

Bob
venkat-Dvenkat-D
Replace == '00Q' line with what I have said to avoid hardcoding
Bob BaconBob Bacon
So here is what that trigger looks like after your suggested replacement and here is the error...

trigger UpdateAccountType on Task (before insert , before update) {
    Set<Id> leadId = new Set<Id>();
    for(Task t : Trigger.new){
        if(t.WhoId != null && t.WhoId.getSobject.Type == ObjectName.sobjettype){
            leadId.add(t.whoId);
        }
    }

    //Query all leads from trigger.new (I removed the Limit 1 --> this will only retrieve the first lead)
    List<Lead> leads = [Select Id, Type__c From Lead Where Id In : leadId];

    //Create map for easy retrieval of records
    Map<Id, Lead> leadMap = new Map<Id, Lead>(leads);

    for(Task tk: Trigger.New){
        //Check if lead is available in Map
        if(leadMap.containsKey(tk.WhoId)){
            Lead lead = leadMap.get(tk.WhoId);  //Get lead from Map
            tk.Account_Type__c = lead.Type__c;
        }
    }
}

Compile Error: Variable does not exist: ObjectName.sobjettype at line 4 column 58

Any suggestions?
venkat-Dvenkat-D
t.WhoId.getSobjectType == Lead.sobjettype - this is the code,

to find which object type a given id is , you can use getsObjecttype method on Id. ex : if you want to know whether given id is account 

you can use Idvar.getsobjecttype == Account.sobjecttype
@Karanraj@Karanraj
If don't want to hardcode the prefix of the object then here is the updated code
trigger UpdateAccountType on Task (before insert , before update) {
    Set<Id> leadId = new Set<Id>();
    for(Task t : Trigger.new){
        if(t.WhoId != null && t.WhoId.getSobjectType == Lead.sobjecttype){
            leadId.add(t.whoId);
        }
    }

    //Query all leads from trigger.new (I removed the Limit 1 --> this will only retrieve the first lead)
    List<Lead> leads = [Select Id, Type__c From Lead Where Id In : leadId];

    //Create map for easy retrieval of records
    Map<Id, Lead> leadMap = new Map<Id, Lead>(leads);

    for(Task tk: Trigger.New){
        //Check if lead is available in Map
        if(leadMap.containsKey(tk.WhoId)){
            Lead lead = leadMap.get(tk.WhoId);  //Get lead from Map
            tk.Account_Type__c = lead.Type__c;
        }
    }
}

 
Bob BaconBob Bacon

Hi S Karanraj,

Thanks for putting this together but I'm still getting an error around hardcode elimination:
Compile Error: Invalid foreign key relationship: Task.WhoId at line 4 column 31

Any Thoughts?

Thanks in advance.

Bob