Hello Friends, In this tutorial, I am going to show you, how you can use Salesforce picklist (drop-down list) value in dynamic SOQL queries. In many cases, we have to use a dynamic SOQL query in Salesforce like batch class or showing filtered record in Lightning Component or in Visualforce page. So without wasting the time let's get started,
Picklist value in Dynamic SOQL query
Use Case: Suppose you have to query Account record where Rating is 'Warm'.
Method No. 1
String sRating='Warm'; String sQuery='SELECT Id, Name FROM Account WHERE Rating=:sRating'; List<Account> lstofAccount=Database.query(sQuery); System.debug('Result^^^'+lstofAccount);Method No. 2
String sQuery='SELECT Id, Name FROM Account WHERE Rating=\'Warm\''; List<Account> lstofAccount=Database.query(sQuery); System.debug('Result^^^'+lstofAccount);In both methods you will get the same result, Hope it will help you a lot.
See also:
Conclusion:
Hope you like this tutorial, for any query or suggestions please feel free to comment.
Thank you.
2 Comments
Quite Simple & Easy to Understand. Thanks!!
ReplyDeleteThanks a lot friend..!
DeletePost a Comment