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

Generating a List of Products NOT Related to Other Objects

We want to clean up our product data, and we want to start by focusing on the products that are NOT related to anything. While we have multiple objects related to products, there are really only 3 main objects that use products, so for simplicity I will be focusing on those. However the problem I'm having is generating this list. With a report I know I am able to filter out products without 2 of our custom objects, but that feature doesn't support filtering out opportunity line items (or even pricebook entries), which is one of the 3 objects we need. I then considered using GROUP BY queries on the 3 objects to find all the ones related to products, and then add all those IDs to a set and then query for all the products that aren't in that set, but this hits the governor limit for query rows very quickly, so that's not an option either. Is there any way at all I could build this list?

Wizno @ ConfigeroWizno @ Configero

Sounds like you have the process down but are just hitting limits... have you tried running it as a batch?

It occurred to me but I am still learning Apex and I've never done batch Apex before. I've read the documentation but it looks like the Database.QueryLocator is for only one query when I'd need to run four queries, and a custom iterator would still have the query limits enforced. I've been trying to find example code for batch Apex with multiple queries but haven't been able to find anything. Would I have to have 4 different batch jobs to make it work?
Wizno @ ConfigeroWizno @ Configero

Can you post the code for the process? I'm sure we can find a way to optimize it a bit and get it figured out. 


Lets say There are Three objects A, B , C which are using PRoduct obj so to delete Products which are not being used in any other object can be done as below : 


Delete [ Select id from Product2 where  id not in (Select productid from A__C) AND  id not in (Select productid from B__C)  And  id not in (Select productid from C__C) ];