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
Andrew_0509Andrew_0509 

SOQL query assistance

I am a bit stuck on a SOQL query.

I need to do the following:

Find the list of Account that have opportunities created from 2015 to 2020 but do have product ABC OR XYZ (opp line item) on those opportunities but in the year 2021 have a product ABC or XYZ on a certain opportunity. 

Any ways to get this done? 

I dont think you can put in IF statements in a SOQL query. 

IF Opp. created date is between 2015 - 2020 then check if it does not have products ABC or XYZ, If Opp. created date is 2020 then check account id with opportunities that have a product ABC or XYZ. 
ShirishaShirisha (Salesforce Developers) 
Hi Andrew,

Greetings!

You would need to first query the products based on the criteria and then you can filter the opportunities based on which you can retrieve accounts.

For this,you would need to create lists as this all can not be done in single query.

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

Thank you!

Regards,
Shirisha Pathuri