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
Aaron HillAaron Hill 

Matching a child object to a parent based on two criteria

We have our leads linked to our accounts through a child/parent relationship. I currently have a trigger set up so that when a lead record is edited it checks the database for accounts that have a matching name and then links them through the "Related Account" lookup field. We have another field called company id though, which is a unique identifier for each company (we use the linkedin company id). This id is on both accounts and leads. I want to change the rule to match FIRST based on this ID and SECOND based on the name if there is no ID Match. I'll try to represent the goal with pseudocode below. And then I'll paste what I've got so far.

WHEN (A lead record is created or updated) {
      IF (Lead company ID is equal to Account company id) {
               //Update lead related account field to matching account.
      } ELSE IF (Lead Name is equal to Account Name) {
               //Update lead related account field to matching account.
      }
}


The pseudocode structure looks different from my actual code.
I tried to use "key: value" maps. This doesn't work. It works for a name match but not for an id match at all. 
I'd appreciate any pointers, thanks!
 
trigger updateRelatedAccount on Lead (before update) {
    set<string> accNames = new set<string>();
    set<Decimal> accLIDs = new set<Decimal>();
        for(Lead ld : Trigger.New){
        	accNames.add(ld.Company);
            accLIDs.add(ld.Company_ID__c);
        }
    
    

        list<Account> accs = new list<Account>([Select Id,Name,LinkedIn_Id__c from account where name=:accNames or LinkedIn_Id__c=:accLIDs]);
        map<String,id> mapaccs = new map<String,id>();
    	map<Decimal, id> mapids = new map<Decimal, id>();

        for(Account ac : accs){
        	mapaccs.put(ac.Name,ac.Id);
            mapids.put(ac.LinkedIn_Id__c,ac.Id);
        }

        for(Lead lds : Trigger.New){
            if (lds.Company_ID__c < 0) {
                lds.Related_Account__c = mapids.get(lds.Company_ID__c);
            } else {
                lds.related_account__c = mapaccs.get(lds.company);
            }
            
            
        	
        }
  }

 
Best Answer chosen by Aaron Hill
Dario BakDario Bak
Hi Aaron! Here are a few changes you should try, please let me know how that goes. Check my comments!

If this works, please consider choosing it as best answer! 
 
trigger updateRelatedAccount on Lead (before update, before insert) { //you NEED before insert too
    set<string> accNames = new set<string>();
    set<Decimal> accLIDs = new set<Decimal>(); //you should try Integer, not sure possible values 
        for(Lead ld : Trigger.New){
        	accNames.add(ld.Company);
            accLIDs.add(ld.Company_ID__c);
        } 
    
    

        list<Account> accs = new list<Account>([Select Id,Name,LinkedIn_Id__c from account where name in :accNames or LinkedIn_Id__c in :accLIDs]); //using WHERE IN
        map<String,id> mapaccs = new map<String,id>();
    	map<Decimal, id> mapids = new map<Decimal, id>();

        for(Account ac : accs){
        	mapaccs.put(ac.Name,ac.Id);
            mapids.put(ac.LinkedIn_Id__c,ac.Id);
        }

        for(Lead lds : Trigger.New){ 
            //you new lead has company id, check if lead has same id
            if (lds.Company_ID__c !=null and mapids.get(lds.Company_ID__c)!=null) { 
                lds.Related_Account__c = mapids.get(lds.Company_ID__c); //if so, copy id
            } else { //if not, search by name
                lds.related_account__c = mapaccs.get(lds.company);
            }
        }
  }

 

All Answers

Dario BakDario Bak
Hi Aaron! Here are a few changes you should try, please let me know how that goes. Check my comments!

If this works, please consider choosing it as best answer! 
 
trigger updateRelatedAccount on Lead (before update, before insert) { //you NEED before insert too
    set<string> accNames = new set<string>();
    set<Decimal> accLIDs = new set<Decimal>(); //you should try Integer, not sure possible values 
        for(Lead ld : Trigger.New){
        	accNames.add(ld.Company);
            accLIDs.add(ld.Company_ID__c);
        } 
    
    

        list<Account> accs = new list<Account>([Select Id,Name,LinkedIn_Id__c from account where name in :accNames or LinkedIn_Id__c in :accLIDs]); //using WHERE IN
        map<String,id> mapaccs = new map<String,id>();
    	map<Decimal, id> mapids = new map<Decimal, id>();

        for(Account ac : accs){
        	mapaccs.put(ac.Name,ac.Id);
            mapids.put(ac.LinkedIn_Id__c,ac.Id);
        }

        for(Lead lds : Trigger.New){ 
            //you new lead has company id, check if lead has same id
            if (lds.Company_ID__c !=null and mapids.get(lds.Company_ID__c)!=null) { 
                lds.Related_Account__c = mapids.get(lds.Company_ID__c); //if so, copy id
            } else { //if not, search by name
                lds.related_account__c = mapaccs.get(lds.company);
            }
        }
  }

 
This was selected as the best answer
Aaron HillAaron Hill
Dario, that worked perfectly thanks! And I really appreciate you adding the comments so I can understand what you did. Best answer, cheers!
Aaron HillAaron Hill
@Dario Bak 

This has been working, but only on an indivisual basis. When I try to update records in bulk using the data import wizard there is no change. Apex triggers can handle bulk records by default right? This worked once before we modified the rule. Do I need to change the trigger?

Thank you!