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
Badar Ali 19Badar Ali 19 

Search based on variable field name

Hi, 

I would like to know that how can we search based on a custom field which we get as a parameter in a method.

For example if I create a custom field Mobilephone for the standard object Account, then how can I search this custom field Mobilephone? 

String phone_number = '1';
String param = 'MobilePhone__c';
Account[] a = [select Name, Id, phone, :param from Account where param =: phone_number];

Both the search field and value to be searched have to be used from the variable.

If you know, please help me with this.

Thanks.

ravi soniravi soni
hi Badar Ali,
I think you need to use Developer Console =>  Edit=> Search In files and put the name whatever you want to search.
it will indicate you where all your search value is used in the class.

don't forget to mark it as best answer if it helps you.
Thank you
PradeepgorePradeepgore
Hi Badar Ali,

Refer snippet below:

// slash is to escape the single quote
String phone_number = '\'1\'';
String param = 'phone';
String query='select Id, Name,'+param+' from Account where '+param+'='+phone_number;

//use debug to check query first
System.debug('query=>'+query);

List<Account> accList=Database.query(query);
System.debug(''+accList);


Additionally we can connect on pradeepgore60@gmail.com

Mark this as best answer if this solves your problem

#DeepDivesIntoSalesforce

Pradeep Gore
bfdhng gfdhbgbfdhng gfdhbg
Oh thanks for sharing these names in the reply sir I was just looking to take the same help for my project I found this guide on this page (https://softenerwaterworld.com/best-water-softener-resin/).