I want to query for Orders that have no related Order Products -- I have queried for related lists before, but I still don't fully understand how, and I feel more puzzled at this since I'm not looking for certain fields on the related products, but rather, specifically looking for a lack of products. How might I go about this?

EDIT: If there is no clean, straight-forward way to query...I don't want to retroactively add a field. My issue is that, in production, there are orders without products, and a trigger is trying to activate it based off of an activation date, and these occasional orders that, for whatever reason, don't have products, are popping up this error.

So, what might my best option be to round up all these orders without products and give them products?

  • 1
    Depending on the volume of data, SOQL does support something like this: SELECT Id FROM Quote WHERE ID NOT IN (SELECT QuoteId FROM QuoteLineItem). You'd probably have the best luck running this in a tool that allows queries to run for a long time, like the Apex Data Loader. – sfdcfox Nov 19 '16 at 17:31

This is one situation that SOQL doesn't really handle too well.

The first thing that comes to my mind would be to create a rollup summary field on Order to count the number of related OrderProducts.

Once you have that information, the query becomes simple.

[SELECT Id FROM Order WHERE <rollup field counting OrderProduct> = 0];
| improve this answer | |

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.