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
TC AdminTC Admin 

How to update product2 total unprocessed opportunity line item when opportunity line item added, updated or deleted

I need some help, I'm trying to figure out how to update products with their total on orders at certain status when the op line ins added, updated, deleted but i start to write some sort of code and it never seems to be right and I end up with total rubbish.

So I have Products BOB, CAT and ELF with stock.
I need to be able to add up ALL the opportunity line items that are at the feild opportunity.sage_status_c is set to  'PROCESSING' and update the product2.unprocessed_orders__c field everytime an opportunity line item is added,edited,deleted.

User-added image

I have this SoQL that I am running outside of SF and pushing up every 5 mins but i need it to happen autoamtically in SalesForce.

any ideas how i get this to work? Has anyone done this before?

TC AdminTC Admin

Sorry, here is the SoQL

SELECT    PricebookEntry.Product2Id ProductID,SUM(aCTUAL_Quantity__C) OnOrder FROM OpportunityLineItem 
WHERE    PricebookEntry.Product2.isactive = TRUE AND (Opportunity_Sage_Status__c like 'NOT READY%'
                OR Opportunity_Sage_Status__c like'PROCESSING%' OR Opportunity_Sage_Status__c like'DESPATCHED%' OR   
                 Opportunity_Sage_Status__c like'%PENDING%' OR Opportunity_Sage_Status__c like'ORDER%')  
                AND Opportunity.O1_2_Return_Reason__c NOT IN ('Defective Non – resellable','Delivery Error Courier Non – resellable',
                'Delivery Error Hacche Non – resellable','Delivery Error Warehouse Non – resellable','Lost by courier - non - sellable')
            
GROUP BY rollup (PricebookEntry.Product2Id ) ORDER BY PricebookEntry.Product2Id  LIMIT 1000