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
tulasi ram 1tulasi ram 1 

how to query a lead based on Phone Number

1. What is the best way to find Lead record based on Phone number.
2. I have a phone number, Based on phone number i need to search a record in salesforce(I dont know which object it is). After that if that record belongs to
    a. Contact it enters into first if loop
    b. Lead it enters into second if loop
    c. Account it enters into third if loop

What is the best way to achieve this. Please suggest me a solution.
 
bhanu_prakashbhanu_prakash
Hi Tulasi,
Mark as best answer, If it resloves !!
you need to SOSL for these trigger requirement,
trigger trg_AccountSOSL on Account(before insert, before update) {
    List < List < SObject >> searchList = [FIND '9999999999 ’IN ALL FIELDS RETURNING Account(Id, Name), Contact, Opportunity, Lead];
    List < account > myAcc = ((List < Account > ) searchList[0]);
    system.debug(myAcc[0].name);
}
or check in anynomus block in developer console 
 
FIND '9999999999'
IN ALL FIELDS
RETURNING Contact(Phone WHERE(Phone =: 9999999999)),
    Account(Phone WHERE(Phone =: 9999999999)),
    Lead(Phone WHERE(Phone =: 9999999999))

Mark as resloved if it helps :) :)
Thanks, 
Bhanu Prakash
visit ForceLearn.com​​  (https://www.forcelearn.com)

 
bhanu_prakashbhanu_prakash
you can use these  too 
FIND {33562156600} IN ALL FIELDS RETURNING Contact(Phone),Account(Phone),Lead(Phone)

 
Niraj Kr SinghNiraj Kr Singh
Hi Tulasi,

You can try this code snipet, and let me know if it works for you !!
List<List<SObject>> searchList = [FIND '1234567890' IN ALL FIELDS
					RETURNING Contact(Id,Name,Phone WHERE(Phone =: '1234567890')),
    				Account(Id,Name,Phone WHERE(Phone =: '1234567890')),
    				Lead(Id,Name,Phone WHERE(Phone =: '1234567890'))];
if((List<Contact>)searchList[0] != null && !((List<Contact>)searchList[0]).isEmpty()){
    System.debug('---Contact: ' + (List<Contact>)searchList[0]);
}
if((List<Account>)searchList[1] != null && !((List<Account>)searchList[1]).isEmpty()){
    System.debug('---Account: ' + (List<Account>)searchList[1]);
}
if((List<Lead>)searchList[2] != null && !((List<Lead>)searchList[2]).isEmpty()){
    System.debug('---Lead: ' + (List<Lead>)searchList[2]);
}
Good Luck !!

Thanks
Niraj
 
Akshay_DhimanAkshay_Dhiman
Hi Tulasi,

 Try This code.
 
List<List<sObject>> searchList = [FIND '9935895698' 
                                  IN ALL FIELDS 
                                  RETURNING 
                                  lead(lastName,phone),
                                  Account(Name,phone),    
                                  Contact(lastName,phone)
                                 ];
lead[] searchLead = (lead[])searchList[0];
Account[] searchAccounts = (Account[])searchList[1];
Contact[] searchContacts = (Contact[])searchList[2];

for (Lead leadObj : searchLead) {
    System.debug('Lead name    '+leadObj.lastname+'    '+leadObj.phone);
}
for (Account accObj: searchAccounts) {
    System.debug('Account name    '+accObj.name+'    '+accObj.phone);
}
for (Contact conObj : searchContacts) {
    System.debug('Contact  name     '+conObj.lastname+'    '+conObj.phone);
}

If you found this answer helpful then please mark it as best answer so it can help others.   

Thanks 
Akshay