You need to sign in to do that
Don't have an account?
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
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
use t.WhoId.getSobject.Type == ObjectName.sobjettype to compare to vaoid hardcoding.
Thanks!
Bob
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?
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
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