You need to sign in to do that
Don't have an account?

apex code to match lead field to account field and then to populate account name of matched account on lead record
i have tried this trigger to match referral__id__c on lead to customer_id__c on account and then to populate account name from matched account to a lookupfield (to account) on lead.....:
here's what i have written -
trigger update_rid on Lead (before insert, before update) {
Set<String> rid = new Set<String>();
for(lead ld : trigger.new){
if(ld.referrer_id__c != null){
rid.add(ld.referrer_id__c);
}
}
if(rid.size() > 0){
map<string, account> cid2rid = new map<string, account>();
for(account acc : [select id,name,customer_id__c from account where name IN :rid]){
cid2rid.put(acc.name, acc);
}
for(lead ld : trigger.new){
if((Trigger.isInsert || trigger.oldMap.get(ld.Id).referrer_id__c != ld.referrer_id__c) && cid2rid.containsKey
(ld.referrer_id__c)){
ld.referer_account_name__c = cid2rid.get(ld.referrer_id__c).account;
}
}
}
}
here's what i have written -
trigger update_rid on Lead (before insert, before update) {
Set<String> rid = new Set<String>();
for(lead ld : trigger.new){
if(ld.referrer_id__c != null){
rid.add(ld.referrer_id__c);
}
}
if(rid.size() > 0){
map<string, account> cid2rid = new map<string, account>();
for(account acc : [select id,name,customer_id__c from account where name IN :rid]){
cid2rid.put(acc.name, acc);
}
for(lead ld : trigger.new){
if((Trigger.isInsert || trigger.oldMap.get(ld.Id).referrer_id__c != ld.referrer_id__c) && cid2rid.containsKey
(ld.referrer_id__c)){
ld.referer_account_name__c = cid2rid.get(ld.referrer_id__c).account;
}
}
}
}
Will the values on the Account be unique and will only return 1 of the id values
for(lead ld : trigger.new){
if(ld.referrer_id__c != null){
rid.add(ld.referrer_id__c);
}
}
if(rid.size() > 0){
map<string, account> cid2rid = new map<string, account >();
//so where you had Name you need customer_id__c in the where clause
for(account acc : [select id,name,customer_id__c from account where customer_id__c IN :rid]){
//also map you need to put the customer_id__c as the key
cid2rid.put(acc.customer_id__c, acc);
}
//now we can link the lead to the Account using the map we created
for(lead ld : trigger.new){
if (ld.referrer_id__c != null && cid2rid.containskey(ld.referrer_id__c))
ld.referer_account_name__c = cid2rid.get(ld.referrer_id__c).id;
}
Try this out and let me know
Have a look at the following:
http://developer.force.com/cookbook/recipe/trigger-pattern-for-tidy-streamlined-bulkified-triggers
https://developer.salesforce.com/page/Trigger_Frameworks_and_Apex_Trigger_Best_Practices?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+force%2Ffeaturedcontent+%28Salesforce+Developers+Featured+Content%29
http://www.embracingthecloud.com/2010/07/08/ASimpleTriggerTemplateForSalesforce.aspx
Also I have a free newsletter and I often talk a lot about triggers, so if you'd like to join send your email to me at sfouracre@selfevolvingsoftware.com
Please let us know if this will help you.
http://amitsalesforce.blogspot.in/2015/06/trigger-best-practices-sample-trigger.html