0

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;
        }
    }    
}

1 Answer 1

0

You can try to deactivate the products instead of deleting them. Just change the isActive to false on the product and on the PriceBookEntries.

Other way to do that is to filter your query:

List<PricebookEntry> PricebookEntries = [SELECT id from PricebookEntry WHERE Product2Id in : PricebookEntryIds and Product2.Make_unavailable_for_opps_and_proposals__c = True AND Id NOT IN (SELECT Product2Id FROM OpportunityLineItem WHERE Product2Id IN : PricebookEntryIds)];

Warning: This won't achieve what you want as it the product will still be available to use on opportunities. It could also hit query limits depending on how much OpportunityLineItems you have.

1
  • Ah that's so much more simple, I can use process builder for that, I didn't realise that worked, doh! Thanks for your help :)
    – Glenn Daly
    Oct 11, 2016 at 10:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .