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
TS-ITWTS-ITW 

Changing the Currency After Deleting the Opportunity Line Items

I have an after update trigger event on Opportunity where I check if a flag has been set to determine if the OLIs need to be deleted so that they can be reloaded by an external call to the API.  My problem is that right after this code executes to delete the OLIs, I need to change the Opportunity currency value from CAD to USD.  When I add the code to change the value, I get the error message below that I believe is caused because I can't update a value in an after update event.  Any suggestions on how I can update the currency field?

OpportunityTrigger: execution of AfterUpdate
caused by: System.FinalException: Record is read-only
Class.OpportunityTriggerHandler.UpdateCurrency: line 180, column 1
Trigger.OpportunityTrigger: line 32, column 1


Here's the method where I delete the OLIs that fires in the after update event on Opp:
//Delete OLIs on an Opportunity when Oppty is updated and DeleteOLI__c is true
    public static void DeleteOLIs (List<Opportunity> NewOpp){
        //get opportunity Ids
        Set<Id> OppId = new Set<Id>();
        for(Opportunity Opp : NewOpp){
            if(Opp.DeleteOLI__c == true){
                OppId.add(Opp.Id);
        }
        }
        
        //get current opportunity line items to delete
        List<OpportunityLineItem> CurrentOLIs = new List<OpportunityLineItem>(
            [Select Id, OpportunityId FROM OpportunityLineItem WHERE OpportunityId IN :OppId]);
            
        //delete Opportunity line items
        if(!CurrentOLIs.isempty()){
            system.debug('CurrentOLIs: ' + CurrentOLIs);
            delete CurrentOLIs;
            
        }
        
    }

 
Vivek DeshmaneVivek Deshmane
Hi,

Either you can update in before update trigger or  
In opportunity after update trigger ,again query the required record and  retrive the list of oppty and change the currency value then update retrived list.

Best Regards,
-Vivek