You need to sign in to do that
Don't have an account?
Jason Adler 9
How to access a field from a custom object from a trigger on Lead object
Attached below is the trigger...its an old trigger that I am digging up now. The goal of the project is to assign an owner to a lead based on postal code. So there are two objects in play: Lead and a custom object called ZIP_Code__c. We have manually assigned a field called Dealer_Lead_Contact__c with an email address to every record in the Zip Code object.
Every time this trigger is ran, on new or updated lead, I want to send an email to the Dealer_Lead_Contact__c. I plan to do this with the process builder but need to first populate an equivalent field in the Lead object.
So, in the trigger, I want to add a functionality that will use the zip code field in the lead object (Zip_Code__c) and search the zip code object (ZIP_Code__c) for a match. If it finds a match, assign the dealer lead contact field in the lead object (Dealer_Lead_Contact__c) with the dealer lead contact in the zip code object (Dealer_Lead_Contact__c)?
Currently the Dealer_Lead_Contact__c is a text field, the email address will be coming in the form of ***@***.com as a column in an excel sheet and uploaded to the Zip code object with the data loader. Not sure if there is a better way to fire an automatic email than using the text as text. Any help is appreciated!
trigger LeadAssignOwnerByPostalCode on Lead (before insert, before update) {
List<Lead> lList = new List<Lead>();
Map<String,ID> zipCodeMap = new Map<String,ID>();
Map<id, User> userMap = new Map<id, User>();
Set<String> userIds = new Set<String>();
Set<String> zipCodeString = new Set<String>();
List<Zip_Code__c> zipCodeList = new List<Zip_Code__c>();
List<Messaging.SingleEmailMessage> listmail = new List<Messaging.SingleEmailMessage>();
for(Lead l: Trigger.new)
{
System.debug('LeadAssignmentByZipCode -- Do_Not_Use_ZipAssignment_del__c ' + l.Do_Not_Apply_Zip_Assignment_Rules__c);
//Do_Not_Use_ZipAssignment_del__c
if(l.Do_Not_Apply_Zip_Assignment_Rules__c == true) continue;
if(Trigger.isUpdate){
System.debug('LeadAssignmentByZipCode -- Update Trigger -- ');
Lead oldLead = Trigger.oldMap.get(l.id);
if(l.PostalCode.equals(oldLead.PostalCode)) continue;
} else
System.debug('LeadAssignmentByZipCode -- Insert Trigger -- ');
if(l.PostalCode <> null){
zipCodeString.add(l.PostalCode);
System.debug('LeadAssignmentByZipCode -- Lead -- ' + l.Name + ' Zip -- ' + l.PostalCode);
}
}
if(!zipCodeString.IsEmpty())
{
System.debug('LeadAssignmentByZipCode -- Leads To Process ' + zipCodeString.size());
zipCodeList = [SELECT id, Name, UserAssigned__c FROM Zip_Code__c WHERE Name IN: zipCodeString ];
} else{
System.debug('LeadAssignmentByZipCode -- No Leads To Process');
}
if(!zipCodeString.IsEmpty()){
System.debug('LeadAssignByZip --- zipCodeString Count --> ' + zipCodeString.size());
for(Zip_Code__c z: zipCodeList){
if(!zipCodeMap.containsKey(z.Name)){
zipCodeMap.put(z.name, z.UserAssigned__c);
userIds.add(z.UserAssigned__c);
}
}
}else
System.debug('LeadAssignByZip --- zipCodeString Count --> zero');
if(!userIds.IsEmpty()){
System.debug('LeadAssignByZip --- userIds Count --> ' + userIds.size());
userMap = new Map<id,User>([SELECT id, email FROM User WHERE id IN: userIDs]);
}else
System.debug('LeadAssignByZip --- userIds Count --> zero');
for(Lead lead: Trigger.New) {
if(lead.Do_Not_Apply_Zip_Assignment_Rules__c == true) continue;
if(Trigger.isUpdate){
System.debug('LeadAssignmentByZipCode -- Update Trigger -- ');
Lead oldLead = Trigger.oldMap.get(lead.id);
if(lead.PostalCode.equals(oldLead.PostalCode)) continue;
} else
System.debug('LeadAssignmentByZipCode -- Insert Trigger -- ');
if(zipCodeMap.containsKey(lead.PostalCode)){
Id owner = zipCodeMap.get(lead.PostalCode);
lead.OwnerId = owner;
String inSubject = 'Lead Assignment Change' ;
String inPlainTextBody = '';
//if(CheckOrganizationID.IsProduction())
// inPlainTextBody = 'A lead has been assigned to you: https://na.salesforce.com/' + lead.id + ' Please click for details.\n\n';
//else
//inPlainTextBody = 'A lead has been assigned to you: ' + CheckOrganizationID.getServer() + '.salesforce.com/' + lead.id + '\n\nPlease click for details.\n\n';
inPlainTextBody = 'A lead has been assigned to you: \n\n' + URL.getSalesforceBaseUrl().toExternalForm() + '/' + lead.Id + '\n\nPlease click for details.\n\n';
inPlainTextBody += 'Name: ' + lead.FirstName + ' ' + lead.LastName + '\n';
inPlainTextBody += 'Company: ' + lead.Company + '\n';
inPlainTextBody += 'Street: ' + lead.Street + '\n';
inPlainTextBody += 'City, State, Zip Code: ' + lead.City + ', ' + lead.State + ' ' + lead.PostalCode + '\n';
if(lead.Email <> null)
inPlainTextBody += lead.Email + '\n';
if(lead.Phone <> null)
inPlainTextBody += lead.Phone + '\n';
String toAddress = '';
Messaging.Singleemailmessage mail = new Messaging.Singleemailmessage();
mail.setSubject(inSubject);
list<String> ToAddr = ToAddress.split(',');
if(userMap.containsKey(owner)){
User u = userMap.get(owner);
//toAddress = u.Email;
}
//mail.setToAddresses(toAddr);
mail.setPlainTextBody(inPlainTextBody);
mail.setSenderDisplayName('Salesforce Administrator');
mail.setSaveAsActivity(false);
mail.setTargetObjectId(owner);
listMail.add(mail);
}
}
if(!listMail.IsEmpty()){
try{
System.debug('LeadAssignByZip Sending Mail listMail Size ---> ' + listMail.size());
Messaging.SendEmailResult [] results = Messaging.sendEmail(listmail);
}catch (Exception Ex)
{
system.debug('ERROR occured while sending email==>'+ String.ValueOf(Ex));
}
} else{
System.debug('LeadAssignByZip listMail Empty No Mail to send..... ');
}
}
Every time this trigger is ran, on new or updated lead, I want to send an email to the Dealer_Lead_Contact__c. I plan to do this with the process builder but need to first populate an equivalent field in the Lead object.
So, in the trigger, I want to add a functionality that will use the zip code field in the lead object (Zip_Code__c) and search the zip code object (ZIP_Code__c) for a match. If it finds a match, assign the dealer lead contact field in the lead object (Dealer_Lead_Contact__c) with the dealer lead contact in the zip code object (Dealer_Lead_Contact__c)?
Currently the Dealer_Lead_Contact__c is a text field, the email address will be coming in the form of ***@***.com as a column in an excel sheet and uploaded to the Zip code object with the data loader. Not sure if there is a better way to fire an automatic email than using the text as text. Any help is appreciated!
trigger LeadAssignOwnerByPostalCode on Lead (before insert, before update) {
List<Lead> lList = new List<Lead>();
Map<String,ID> zipCodeMap = new Map<String,ID>();
Map<id, User> userMap = new Map<id, User>();
Set<String> userIds = new Set<String>();
Set<String> zipCodeString = new Set<String>();
List<Zip_Code__c> zipCodeList = new List<Zip_Code__c>();
List<Messaging.SingleEmailMessage> listmail = new List<Messaging.SingleEmailMessage>();
for(Lead l: Trigger.new)
{
System.debug('LeadAssignmentByZipCode -- Do_Not_Use_ZipAssignment_del__c ' + l.Do_Not_Apply_Zip_Assignment_Rules__c);
//Do_Not_Use_ZipAssignment_del__c
if(l.Do_Not_Apply_Zip_Assignment_Rules__c == true) continue;
if(Trigger.isUpdate){
System.debug('LeadAssignmentByZipCode -- Update Trigger -- ');
Lead oldLead = Trigger.oldMap.get(l.id);
if(l.PostalCode.equals(oldLead.PostalCode)) continue;
} else
System.debug('LeadAssignmentByZipCode -- Insert Trigger -- ');
if(l.PostalCode <> null){
zipCodeString.add(l.PostalCode);
System.debug('LeadAssignmentByZipCode -- Lead -- ' + l.Name + ' Zip -- ' + l.PostalCode);
}
}
if(!zipCodeString.IsEmpty())
{
System.debug('LeadAssignmentByZipCode -- Leads To Process ' + zipCodeString.size());
zipCodeList = [SELECT id, Name, UserAssigned__c FROM Zip_Code__c WHERE Name IN: zipCodeString ];
} else{
System.debug('LeadAssignmentByZipCode -- No Leads To Process');
}
if(!zipCodeString.IsEmpty()){
System.debug('LeadAssignByZip --- zipCodeString Count --> ' + zipCodeString.size());
for(Zip_Code__c z: zipCodeList){
if(!zipCodeMap.containsKey(z.Name)){
zipCodeMap.put(z.name, z.UserAssigned__c);
userIds.add(z.UserAssigned__c);
}
}
}else
System.debug('LeadAssignByZip --- zipCodeString Count --> zero');
if(!userIds.IsEmpty()){
System.debug('LeadAssignByZip --- userIds Count --> ' + userIds.size());
userMap = new Map<id,User>([SELECT id, email FROM User WHERE id IN: userIDs]);
}else
System.debug('LeadAssignByZip --- userIds Count --> zero');
for(Lead lead: Trigger.New) {
if(lead.Do_Not_Apply_Zip_Assignment_Rules__c == true) continue;
if(Trigger.isUpdate){
System.debug('LeadAssignmentByZipCode -- Update Trigger -- ');
Lead oldLead = Trigger.oldMap.get(lead.id);
if(lead.PostalCode.equals(oldLead.PostalCode)) continue;
} else
System.debug('LeadAssignmentByZipCode -- Insert Trigger -- ');
if(zipCodeMap.containsKey(lead.PostalCode)){
Id owner = zipCodeMap.get(lead.PostalCode);
lead.OwnerId = owner;
String inSubject = 'Lead Assignment Change' ;
String inPlainTextBody = '';
//if(CheckOrganizationID.IsProduction())
// inPlainTextBody = 'A lead has been assigned to you: https://na.salesforce.com/' + lead.id + ' Please click for details.\n\n';
//else
//inPlainTextBody = 'A lead has been assigned to you: ' + CheckOrganizationID.getServer() + '.salesforce.com/' + lead.id + '\n\nPlease click for details.\n\n';
inPlainTextBody = 'A lead has been assigned to you: \n\n' + URL.getSalesforceBaseUrl().toExternalForm() + '/' + lead.Id + '\n\nPlease click for details.\n\n';
inPlainTextBody += 'Name: ' + lead.FirstName + ' ' + lead.LastName + '\n';
inPlainTextBody += 'Company: ' + lead.Company + '\n';
inPlainTextBody += 'Street: ' + lead.Street + '\n';
inPlainTextBody += 'City, State, Zip Code: ' + lead.City + ', ' + lead.State + ' ' + lead.PostalCode + '\n';
if(lead.Email <> null)
inPlainTextBody += lead.Email + '\n';
if(lead.Phone <> null)
inPlainTextBody += lead.Phone + '\n';
String toAddress = '';
Messaging.Singleemailmessage mail = new Messaging.Singleemailmessage();
mail.setSubject(inSubject);
list<String> ToAddr = ToAddress.split(',');
if(userMap.containsKey(owner)){
User u = userMap.get(owner);
//toAddress = u.Email;
}
//mail.setToAddresses(toAddr);
mail.setPlainTextBody(inPlainTextBody);
mail.setSenderDisplayName('Salesforce Administrator');
mail.setSaveAsActivity(false);
mail.setTargetObjectId(owner);
listMail.add(mail);
}
}
if(!listMail.IsEmpty()){
try{
System.debug('LeadAssignByZip Sending Mail listMail Size ---> ' + listMail.size());
Messaging.SendEmailResult [] results = Messaging.sendEmail(listmail);
}catch (Exception Ex)
{
system.debug('ERROR occured while sending email==>'+ String.ValueOf(Ex));
}
} else{
System.debug('LeadAssignByZip listMail Empty No Mail to send..... ');
}
}
I have updated your code as per your requirement. I have added the comments like Added/Updated by Abhishek wherever i have made the updates. Please find the updated code below:
Let me know if you face any issue with this.
Thanks,
Abhishek Bansal.
Gmail: abhibansal2790@gmail.com
Skype: abhishek.bansal2790
Phone: +917357512102