0

I'm making a Trigger that when adding a product to the Opportunities object when updating the Quantity if placed below the original value an error will appear, but when increasing the date field it would have to be updated automatically to the current day

but it's giving an error, what could I be doing or what am I doing wrong?

OpportunityLineItemTrigger: execution of BeforeUpdate caused by: System.FinalException: Record is read-only Class.OpportunityLineItemHelper.checkChangeQuantity: line 11, column 1

public static void checkChangeQuantity(Map<Id, OpportunityLineItem> oldMap, Map<Id, OpportunityLineItem> newMap) {
    for (OpportunityLineItem newItem : newMap.values()) {
        OpportunityLineItem oldItem = oldMap.get(newItem.Id);

        if (newItem.Quantity > oldItem.Quantity) {
            Trigger.newMap.get(newItem.Id).addError(System.Label.IncreaseQuantityError);
        }
        else if (newItem.Quantity < oldItem.Quantity) {
            newItem.ServiceDate = Datetime.now().date();
        }
    }
}

public static List<Opportunity> checkItemChange(Map<Id, OpportunityLineItem> oldMap, Map<Id, OpportunityLineItem> newMap) {
    Set<Id> opportunityIdSet = new Set<Id>();

    for (OpportunityLineItem newItem : newMap.values()) {
        OpportunityLineItem oldItem = oldMap.get(newItem.Id);

        if ((newItem.Quantity != oldItem.Quantity) || (newItem.UnitPrice != oldItem.UnitPrice)) {
            opportunityIdSet.add(newItem.opportunityId);
        }
    }

    List<Opportunity> opportunityList = [
        SELECT Id, StageName
        FROM Opportunity
        WHERE Id IN: opportunityIdSet
        AND StageName <> 'Prospecting'
    ];

    for (Opportunity opp : opportunityList) {
        opp.StageName = 'Prospecting';
    }
    return opportunityList;
}
2
  • 1
    Deleting and re-asking your question isn't a good way to use this site, so please don't do that again. That said, I did disagree with the commenter on the potential duplicate (at least in part. The bit about searching for similar questions before making one of your own was good advice.) The suggested duplicate was clearly about an after trigger whereas this question is clearly about encountering this error in a before trigger.
    – Derek F
    Commented Apr 14, 2023 at 16:28
  • Sorry I'm new in this field, I deleted it because I found a topic with the answer, so I thought it was necessary to leave the question, but this error appeared in the same code that I didn't find anything to solve Commented Apr 14, 2023 at 16:43

1 Answer 1

1

The most likely cause of this error is that you're passing in Trigger.oldMap and Trigger.newMap in the incorrect order. Trying to update a record in Trigger.oldMap results in this exception for any type of trigger. You can only call addError on those records, and only in a delete (either before or after) trigger context. Check the OpportunityLineItemTrigger trigger where you're calling this method from and verify the order of the parameters are correct.

4
  • 2
    adding an error on an oldmap record in a before delete context is the exception (lol) to that rule, I believe.
    – Derek F
    Commented Apr 14, 2023 at 16:45
  • @DerekF you're right, that wasn't properly worded.
    – sfdcfox
    Commented Apr 14, 2023 at 16:46
  • @sfdcfox the addError part is working fine, the part itself that is indicating an error is when updating the date I have a problem with this part else if (newItem.Quantity < oldItem.Quantity) { newItem.ServiceDate = Datetime.now().date();} Commented Apr 14, 2023 at 17:08
  • @KauãRoberto The addError method is being called on Trigger.newMap(...), which is allowed, but newMap may not be Trigger.newMap, hence the error. I'm almost certain the error is in your trigger code, not your trigger handler.
    – sfdcfox
    Commented Apr 14, 2023 at 17:14

You must log in to answer this question.

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