I have a SOQL query with a lot of filters and I want to group the results and process objects which match a certain filter. For eg. I could have a query where I want to process different kind of leads differently,

List<Lead> leads = [Select id,name,ownerId from Lead where rating = 'Hot' or leadsource='Web' or name like 'Sam%'];

For rating = 'Hot' , I want to change the ownerId to 'X'.

For leadSource ='Web', I want to change the ownerId to 'Y'

For name like 'Sam%', I want to change to a queue

I can think of 2 ways of doing this -

1) To have different queries for individual filters and process the objects

2) Or to loop over the results and have an if/else statement to identify the action

Is there any other way to optimize it? Can I group the results based on filters?

  • Option 2 with if else is better. – Raul Oct 22 '17 at 10:47

Lead assigment rules not be the best in your situation?


  • No, want to do it using apex since there is a lot of other processing also happening on the results. – user1104 Oct 22 '17 at 13:14
  • One does not exclude the other in my opinion. – Adamo Oct 23 '17 at 17:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.