You need to sign in to do that
Don't have an account?
Aaron 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!
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); } } }
If this works, please consider choosing it as best answer!
All Answers
If this works, please consider choosing it as best answer!
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!