How to form a query using StartsWith?

322    Asked by DianeCarr in Salesforce , Asked on Aug 28, 2023

 While doing a test class, I inserted a product record in the test setup and another product record in the method, but if I query the product, then I have to go to filter to get the record that I want to assert against. What is its syntax if I want to filter the Product Name using SOQL StartsWith? Can anyone please help me with how to write a query using SQL starts with?

Answered by Elizabeth Clarke

If you think that Like query is safe, and you use it for any other object, like opportunity, and then an admin adds workflow or code to make a standard naming format for the record, which is a common practice, in that case, your test will fail because the name was changed and the records will not be found via the Like operator.

If the product is created during setup, then we can use ID != {Method Product ID} or If the product is created during the method, we can use id = {Method Product ID}. It would be best to use the ID to query for the record, as it will ensure that your test can survive any changes made in the org that are not in your control.


Your Answer

Interviews

Parent Categories