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
Nevin O'Regan 3Nevin O'Regan 3 

Update Approval Status Field based on OpportunityLineItem Sales Price

I have an Approval_Status__c field on the Opportunity. When a related OpportunityLineItem.UnitPrice is below the List Price I need this to update to 'Needs Approval'. If the Unit Price is above the List Price then the Approval_Status__c field should be null. 
Can anyone give me some help with wrighting a trigger for this?
Krishna SambarajuKrishna Sambaraju
Hi Nevin,
Here is the code for the trigger.
trigger updateOppApprovalStatus on OpportunityLineItem (after insert, after update) {
	List<Opportunity> opps = new List<Opportunity>();
    for (OpportunityLineItem oli : trigger.new)
    {
        if (oli.UnitPrice < oli.ListPrice)
        {
            opps.add(new Opportunity(Id = oli.OpportunityId, Approval_Status__c = 'Needs Approval'));
        }
        else
        {
            opps.add(new Opportunity(Id = oli.OpportunityId, Approval_Status__c = null));
        }
    }
    if (opps.size() > 0)
    {
        update opps;
    }
}
Hope this helps.
 
Deepali KulshresthaDeepali Kulshrestha
Hi Nevin,
Greetings to you!

Trigger : - 
trigger updateApprovalStatus on OpportunityLineItem (before insert, before update) {
        if((trigger.isBefore && trigger.isInsert) || (trigger.isBefore && trigger.isUpdate)){
            updateApprovalStatusClass.updateApprovalStatusMethod(trigger.new);
        }
    }
    
Class : - 

    public class updateApprovalStatusClass {
        public static void updateApprovalStatusMethod(List<OpportunityLineItem> oliList)
        {
            try{
                if(!oliList.isEmpty()){
                List<Opportunity> oppList = new List<Opportunity>();
                for (OpportunityLineItem oli : oliList)
                    {
                        Opportunity oppInst = new Opportunity();
                        if(oli.OpportunityId!=null){
                            oppInst.Id = oli.OpportunityId;
                            if (oli.UnitPrice < oli.ListPrice)
                            {
                                oppInst.Approval_Status__c = 'Needs Approval';
                            }
                            else
                            {
                                oppInst.Approval_Status__c = null;
    
                            }
                            oppList.add(oppInst);
                        }
                        if (!oppList.isEmpty())
                        {
                            update oppList;
                        }
                    }
                }
            }
            catch (DmlException dee)
            {
                System.debug('DmlException Message' + dee.getLineNumber() + 'DmlException Message = ' +dee.getMessage());

            }catch (Exception ee) {
                System.debug('Exception Message' + ee.getLineNumber() + 'Exception Message = ' + ee.getMessage());
            }

        }
    }

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha.