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
Salesforce Admin 110Salesforce Admin 110 

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

}
}
netspidernetspider
ok I can see a few issues with the code
Will the values on the Account be unique and will only return 1 of the id values
Salesforce Admin 110Salesforce Admin 110
hi netspider, thanks for reading.....yes the values in account field customer_id__c are unique and i would expect just 1 value to be returned....what are the issues with the code?
netspidernetspider
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 >();
//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
Salesforce Admin 110Salesforce Admin 110
thanks a million netspider, i will check this out asap and hopefully can award you with best answer
netspidernetspider
Also you should look at employing a trigger pattern which gives better management to your triggers and gurantees order of execution etc

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 
Amit Chaudhary 8Amit Chaudhary 8
Please try below code. Below trigger will take care about Insert and Update event also. And in Case of Update will only fire when referrer_id__c field will change

User-added image
Please let us know if this will help you.
http://amitsalesforce.blogspot.in/2015/06/trigger-best-practices-sample-trigger.html