Skip to main content

Scenario is:

 

On" before update" and 'before insert" of opportunityLineitem, a custom field should not get inserted and updated after saving the product details and throw an error saying that " You are not allowed to update this field ".

 

 i tried doing null check on this field  and show the error in my trigger but its showing error -  "sobject row does not allow errors " so i checked the field level security and it has read and write access but still this is coming

 

So if any one could suggest any other alternative approach to achieve this use case using triggers only would be highly appreciated.

 

Thanks,

Mahi

4 respuestas
  1. 13 may 2023, 02:18

    @Mahi Tangnoo,

    Please try this code:

    trigger OliTrigger on OpportunityLineItem (before insert, before update) {

    Map<Id, OpportunityLineItem> lineItemsToUpdateMap = new Map<Id, OpportunityLineItem>();

    for (OpportunityLineItem lineItem : Trigger.new) {

    if (lineItem.Product2.Name == 'Implementation Services' && (Trigger.oldMap.get(lineItem.Id).Y_2__c != lineItem.Y_2__c)) {

    lineItemsToUpdateMap.put(lineItem.Id, lineItem);

    }

    }

    if (!lineItemsToUpdateMap.isEmpty()) {

    List<OpportunityLineItem> lineItemsToUpdate = new List<OpportunityLineItem>(lineItemsToUpdateMap.values());

    for (OpportunityLineItem lineItem : lineItemsToUpdate) {

    lineItem.addError('You are not allowed to update the Y2 field.');

    }

    }

    }

0/9000