Autopopulate Lookup Field based on Phone Number - Answers - Salesforce Trailblazer Community
Trailblazer Community
Ask Search:
Jeffery WrightJeffery Wright 

Autopopulate Lookup Field based on Phone Number

Hi there,

I have a custom object of Phone Calls. These are records imported from Verizon and I want to match the phone number of these calls to the respective Lead that has that specific number. I already have a Lookup field, I just need to edit the Phone Call records to populate that Lookup field based on the Phone Number which is the matching record. How am I able to do this?
Best Answer chosen by Jeffery Wright
Joseph OrcioJoseph Orcio
Hi Jeff - You can probably use Flow for this requirement then.  It looks like the example in the "Build a simple flow" trailhead below is what you are looking for.


Flow
https://help.salesforce.com/articleView?id=flow.htm&type=5
https://help.salesforce.com/articleView?id=flow_builder.htm&type=5

Trailhead/To Learn How To Use Flows:
https://trailhead.salesforce.com/en/content/learn/trails/build-flows-with-flow-builder
https://trailhead.salesforce.com/en/content/learn/projects/build-a-simple-flow

 

All Answers

Joseph OrcioJoseph Orcio
If I understand correcty, you have a Phone Call object that has a Lookup to Leads. If you are trying to populate the Lookup field with the lead record, that matches based on the phone number then this anonymous apex might help:

A few key assumptions I made, are:
  • Phone Numbers are unique across all contacts
  • The custom object name & field names are called Phone_Call__c.Phone__c.


 
list<Contact> contacts = [select id, phone from lead where phone !=null];
list<Phone_Call__c>  phonecalls = [select id, lead__c, phone__c from phone_call__c];

list<Phone_Call__c> phoneCallsToUpdate = new list<Phone_Call__c>(); 

for(Contact c : contacts) {
    for(Phone_Call__c phoneCall : phonecalls) {
         if (phoneCall.phone__c == c.phone) {
             phoneCall.lead__c = c.Id;

             phoneCallsToUpdate.add(phoneCall);  
         }
    }
}


if (phoneCallsToUpdate.size()>0){
   update phoneCallsToUpdate;
}

 
Jeffery WrightJeffery Wright
Thank you for the apex code, but I'm unfamiliar with how to implement it.
Jeffery WrightJeffery Wright
I'm supposing this? https://help.salesforce.com/articleView?id=code_dev_console_execute_anonymous.htm&type=5
Jeffery WrightJeffery Wright
So I'm running Professional edition and it appears that's the one version that doesn't allow me to run Apex code. Is there a different way I can implement this?
Joseph OrcioJoseph Orcio
Hi Jeff - You can probably use Flow for this requirement then.  It looks like the example in the "Build a simple flow" trailhead below is what you are looking for.


Flow
https://help.salesforce.com/articleView?id=flow.htm&type=5
https://help.salesforce.com/articleView?id=flow_builder.htm&type=5

Trailhead/To Learn How To Use Flows:
https://trailhead.salesforce.com/en/content/learn/trails/build-flows-with-flow-builder
https://trailhead.salesforce.com/en/content/learn/projects/build-a-simple-flow

 
This was selected as the best answer
Jeffery WrightJeffery Wright
I've dabbled with flows before--they're a little scary--but I suppose now it's time I get into them fully. Thanks, I'll try this out soon.
Jeffery WrightJeffery Wright
This is more "Add a Contact if it doesn't already exist" but I'll try wading into this as an example flow. I'll submit more questions for help as I need it on a different thread.