I want process builder to delete a pricebook entry if the product record meets a certain criteria. I've created something that does this by invoking apex to delete the pricebook entry record. However, if the product is attached to an opportunity, it falls over. Is there anything I can do in process builder to rule out products being used with opportunities, or is there something I can do with the code so stop it firing an error.
public class DeletePricebookEntry{
@invocableMethod
public static void PricebookEntryDelete(List<Id> PricebookEntryIds){
List<PricebookEntry> PricebookEntries = [SELECT id from PricebookEntry WHERE Product2Id in : PricebookEntryIds and Product2.Make_unavailable_for_opps_and_proposals__c = True];
if(!PricebookEntries.isEmpty()){
delete PricebookEntries;
}
}
}