0

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){

            productCodeList.add(itm.Product_Code__c);
        }
        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
    CustomerCode__c=:trigger.new[0].Sold_To_Party_Code__c];

        Exchange_Rate__c ct = [select ExchangeRate__c,FromCurrency__c from Exchange_Rate__c where FromCurrency__c='USD' AND
    createddate=Today];

        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);

                    if(itm.DCNR_Projected_Cost__c>=85){

                        op.Check_Approval_Value__c = itm.DCNR_Projected_Cost__c;


                    }

                    op.Id = itm.OpportunityId;
                    oppList.add(op);

                    }
            }
            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>();
        accmap.putall(oppList);
        if(accmap.size()>0){

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

        }
        } }}

1 Answer 1

0

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!

1
  • Thanks a lot, Hengky!!!
    – jsmith
    Commented Jun 8, 2020 at 15:48

You must log in to answer this question.

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