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