+ Start a Discussion
Aaron HillAaron Hill 

Trigger to Relate Lead and Account based on Multiple criteria

I have a trigger already mostly written that relates a lead to a parent account object based on 2 criteria. The first is it's linkedin company id. So some of our leads and accounts have as a record their database id on linkedin. If a lead LID matches an account LID I want the lead to update it's related account to be that account with the same id. The SECOND criteria is name, so if the names match- the lead will update with an account with a matching name. I only want a name match search to happen if the id match doesn't happen. I'll post below what I have so far.
trigger updateRelatedAccount on Lead (before update) {
    set<string> accNames = new set<string>();
    set<Decimal> accLIDs = new set<Decimal>();
        for(Lead ld : Trigger.New){

        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){

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

I was patting myself on the back because I really REALLY thought this would work. It seems like it would. The mapids object should contain the LID as the key and then the related account id as the Value pair. 

If I do a name match it works perfectly. But if I try to do a LID match it fails. Can anyone help me out? Thanks!
Hey Aaron, have you tried adding some debugs to see if the map actually contains a matching key?

For instance if you add the below debug statement within the if statement and run the trigger in debug what is the output true or false? If false it means that account query did not pull any linkedin id value that matches what is stored in the lead record.

Also do both fields in account and lead object have a data type of Number? You are checking if lds.Company_Id__c is less than zero, does that mean the values are negative numbers like -123578 ?
​for(Lead lds : Trigger.New){
 if (lds.Company_ID__c < 0) {
   system.debug('Contains Key:  ' + mapsids.containsKey(lds.Company_ID__c))
 lds.Related_Account__c = mapids.get(lds.Company_ID__c);
 } else {
    lds.related_account__c = mapaccs.get(lds.company);