+ Start a Discussion

Creating SOQL query in API without hardcoding ids or string

For an external user to pull records associated with a specific field (say, event for example) amongst a large group of records associated to other events, how would I include that in my method without making a hardcoded reference like the following?

[Select Id, EventName, EventID, Name, Email FROM Registration WHERE EventID = 'abc4e00000KlMnO']

since the following 

[Select Id, EventName, EventID, Name, Email FROM Registration] 

pulls the records from all events and not the one of interest.

ShirishaShirisha (Salesforce Developers) 


You would need to query the Event records which are required and store it in the list so that you can use that list as a binding variable instead of hard coded record Id.
// Create a list of account records from a SOQL query
List<Account> accts = [SELECT Id, Name FROM Account ];

Then you can use the list in the SOQL Query as below:
[Select Id, EventName, EventID, Name, Email FROM Registration WHERE EventID = :accnts.Id]

Please mark it as best answer if it helps you to fix the issue.

Thank you!

Shirisha Pathuri