The scenario is, there is a checkbox type field on Opportunity object called "Change_in_product_price__c". when this field value is 'TRUE' and When the user tries to add another OpportunityLineItem to the same opportunity. It should through an error message. I have Before Insert trigger on OpportunityLineItem object as below. Please suggest what condition I should put in the trigger to solve this.Thanks in advance.

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

        set<string> productCodeList = new set<string>();
        List<opportunity> oppList = new List<opportunity>();
            if(Trigger.isBefore) {
        if(Trigger.IsInsert) {
         for(OpportunityLineItem itm : trigger.new){

        List<DCNR_Projections__c> DCP = [SELECT ID, DC__c, Ratio__c, ITEMCD__c,DC_Per_Pack__c FROM DCNR_Projections__c
                                         WHERE ITEMCD__c IN:productCodeList AND

        Exchange_Rate__c ct = [select ExchangeRate__c,FromCurrency__c from Exchange_Rate__c where FromCurrency__c='USD' AND

        for(DCNR_Projections__c dc: DCP) {

            opportunity op = new opportunity();
            op.Check_Approval_Value__c = 1;

            for(OpportunityLineItem itm : trigger.new){

                if(dc.ITEMCD__c == itm.Product_Code__c){
                Decimal INR = dc.Ratio__c*itm.UnitPrice*ct.ExchangeRate__c;
                Decimal DCNR2 = dc.DC_Per_Pack__c/INR*100;
                itm.DCNR_Projected_Cost__c = DCNR2.setScale(2);


                        op.Check_Approval_Value__c = itm.DCNR_Projected_Cost__c;


                    op.Id = itm.OpportunityId;

            system.debug('checkApproval** '+op.Check_Approval_Value__c);
            //opportunity opt = [select id,Change_in_product_price__c from opportunity where id=:trigger.new[0].OpportunityId];
           //system.debug('check in product price '+opt.Change_in_product_price__c);
        map<id,opportunity> accmap = new map<id,opportunity>();

            update accmap.values();
            //update oppList;

        } }}

Since Opportunity always gets updated whenever there is any change in the OpportunityLineItems (reason), that means you can put the validation in Opportunity trigger (before update) instead. And there is a Boolean flag that you can make use: HasOpportunityLineItem which returns true if there are already line item(s) added to the opportunity.

trigger OpportunityTrigger on Opportunity (before update) {

    if (trigger.isBefore) {
         if (trigger.isUpdate) {
             for (Opportunity opp : trigger.new) {
                 if (trigger.oldMap.get(opp.Id).Change_in_Product_Price__c
                        && opp.Change_in_Product_Price__c
                        && opp.HasOpportunityLineItem) {
                    opp.addError('Uh oh! Can\'t add product anymore!');

Hope it helps!

  • Thanks a lot, Hengky!!!
    – jsmith
    Jun 8 '20 at 15:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.