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
JustinWilliams2381JustinWilliams2381 

trying to get started on my first Apex code... feel close but probably so far.

I need to run a trigger before insert, before update on leads.

 

I need to grab the value from a custom field in the lead record that was just updated/inserted.  Lead.customFieldValue

 

I then need to query the accounts table and find a match e.g.

 

Account[] accountId = [SELECT Id FROM Account WHERE Account.customField__c = Lead.customFieldValue];

 

I then need to take that account record ID that was a match to my lead record and place that value into the custom relationship field on the lead record called "Account".  

 

Basically I just took a lead, found an account record whose custom field value matched the lead's custom field value and linked the lead up to the Account record

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Ritesh AswaneyRitesh Aswaney

Here you go... give it a whirl...

 

trigger LeadBefore on Lead (before insert, before update) 

{

 

Map<String, Lead> leadMatches = new Map<String, Lead>{};

 

for (Lead ld : trigger.new)

leadMatches.put(ld.customField__c, ld); //make a map of customField, Lead

 

for( Account acc : [SELECT Id, customField__c FROM Account WHERE customField__c IN :leadMatches.keySet()])

leadMatches.get(acc.customField__c).account__c = acc.Id;

 

}

All Answers

Ritesh AswaneyRitesh Aswaney

Here you go... give it a whirl...

 

trigger LeadBefore on Lead (before insert, before update) 

{

 

Map<String, Lead> leadMatches = new Map<String, Lead>{};

 

for (Lead ld : trigger.new)

leadMatches.put(ld.customField__c, ld); //make a map of customField, Lead

 

for( Account acc : [SELECT Id, customField__c FROM Account WHERE customField__c IN :leadMatches.keySet()])

leadMatches.get(acc.customField__c).account__c = acc.Id;

 

}

This was selected as the best answer
JustinWilliams2381JustinWilliams2381

It Whirled!  Well after a little fixing any way.  Actually I'm not entirely clear why what I did fixed it but it had something to do with the Map method requiring two types instead of the one you had in your code.  No clue if that's because you missed it or that was something unique to my case but it works great.

 

I also added one little thing to the SOQL to make sure it only worked for one business channel and not another.  Thank you SO much.

 

Here is my final code if you wanted to review.

 

1
2
3
4
5
6
7
8
9
trigger updateGradeBeamLead on Lead (before insert, before update){
    map<Decimal,Lead> leadMatches = new Map<Decimal,Lead>{};
        for (Lead Id : trigger.new)
          leadMatches.put(Id.GB_Record_Id__c, Id); //make a map of customField, Lead
   
      for( Account acc : [SELECT Id, GB_Record_Id__c FROM Account WHERE GB_Record_Id__c <> NULL AND GB_Record_Id__c IN :leadMatches.keySet()])

leadMatches.get(acc.GB_Record_Id__c).Account__c = acc.Id;
}
Ritesh AswaneyRitesh Aswaney
Well done!