+ Start a Discussion
Andrew Hoban 6Andrew Hoban 6 

Populate Lookup Field Based on Custom Field Join


I have an apex calss that has a list of records within a custom object. One of the fields within this object is Email_Address__c. There is also a lookup field to Account which is currently null.

Before I insert the custom object records, i need to find the corresponding account record based on email address. If there is an account with the same email address, i need to add the account id to the custom object list to populate the lookup field.

The example below has a list of records within the custom object and list of accounts - ive removed any duplicate accounts that have the same email address.

How would I join these two lists to add the account id to the custom object record?
List<Account> accList = [SELECT Id, Name, PersonEmail, CreatedDate, RecordTypeId FROM ACCOUNT];
List<Custom_Object__c> cusObjList = [SELECT Id, Account__c, Email_Address__c FROM Custom_Object__c]; 

Map<String,Account> mapStrByemail=new Map<String,Account>();

for(Account acc:accList){
    mapStrByemail.put(acc.PersonEmail,acc);//only bring back unique emails 

ShirishaShirisha (Salesforce Developers) 
Hi Andrew,


Hope this thread helps you to achieve your requirement to populate the field.


Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
Andrew Hoban 6Andrew Hoban 6
Hi Shirisha,

This doesnt do what im looking for unfortunately. In my rerquirement, im trying to populate a lookup field by obaining the account id via a join on the same email address value.