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 

Delete trigger on Opportunity won't update Product record

Hi all,

I'm creating an auto roundup for stock ramining after Opportunites are created, update and deleted. However i can't get the delete trigger to update the fields on the product record. When the trigger runs there are no error messages, it just seems to stop running before triggering the if statements which decide what to update the product with.
 
trigger UpdateLineItemsOnDelete on Opportunity (before delete) {
    for (Opportunity OP : trigger.old) {
        system.debug('Delete OP trigger started');
        List<OpportunityLineItem> LineItemsOnDel = [SELECT Id, Product2ID, Op_Sage_Status_Update__c
                                                      FROM OpportunityLineItem
                                                     WHERE OpportunityId = :OP.id];
        system.debug('Check LineItemsOnDel ' + LineItemsOnDel);
        for (OpportunityLineItem OPL : LineItemsOnDel) {
        system.debug('OPL ' + OPL);
        List<OpportunityLineItem> RealLineItem = [SELECT Id
                                                    FROM OpportunityLineItem
                                                   WHERE Product2ID = :OPL.Product2Id AND Id <> :OPL.Id
                                                   LIMIT 1];
        system.debug('Check RealLineItem ' + RealLineItem);
            if(RealLineItem.isempty()) {
               Product2 PR = [SELECT Id, Other_Details__c, Technical_Details__c
                                FROM Product2
                                WHERE Id = :OPL.Product2Id];
               system.debug('Check if product queried fine ' + PR);
                if(OPL.Op_Sage_Status_Update__c == 'Processing%' && OPL.Op_Sage_Status_Update__c == 'Despatched%' 
                   && OPL.Op_Sage_Status_Update__c == 'Not Ready%' && OPL.Op_Sage_Status_Update__c == 'Order%' && OPL.Op_Sage_Status_Update__c == 'Pending%'){
                    PR.Other_Details__c = '0';
                    system.debug('Non forecast if statement triggered');
                    UPDATE PR;
                }
                if(OPL.Op_Sage_Status_Update__c == 'forecast%'){
                    PR.Technical_Details__c = '0';
                    system.debug('Forecast if statement triggered');
                    UPDATE PR;
                }
            }
            else{
                system.debug('Else statement triggered');
                update RealLineItem;
            }
        }
    }
}
If anyone has any ideas they would be greatly appreciated, thanks!
 
ShirishaShirisha (Salesforce Developers) 
Hi,

Greetings!

Do you have values on the field "Op_Sage_Status_Update__c" like forecast% ,Processing% and etc.I would suggest you to remove the from the values with which you are comparing on the if statements.

If the condition doesn't met then the logic in the code will not fire.

Please mark it as best answer if it helps you to fix the issue.

Regards,
​​​​​​​Shirisha Pathuri