0
trigger duplicatecode on OpportunityLineItem (before insert) {
{
    Set<String> setProductCode= new Set<String>();
    For(OpportunityLineItem nam : trigger.new)
    {
        setProductCode.add(nam.ProductCode);
    }
   if(setProductCode.size() > 0 )
    {
       List<OpportunityLineItem> oppo = [select productcode,id from OpportunityLineItem where ProductCode in :setProductCode ];

        Map<String ,OpportunityLineItem> mapNameWiseoppo = new Map<String,OpportunityLineItem>();
        For(OpportunityLineItem nam : oppo)
        {
            mapNameWiseoppo.put(nam.ProductCode ,nam);
        }
        For(OpportunityLineItem nam : trigger.new)
        {
            if(mapNameWiseoppo.containsKey(nam.ProductCode))
            {    
                nam.Name.addError('code already Exist ');    
            }    
        }    
    }    
}    
}
  • Can you explain what do you mean by "its not updating"? – kurunve Dec 5 '18 at 10:07
  • its not showing error whenever i add duplicate product code – user62552 Dec 5 '18 at 10:12
  • The OpportunityLineItems are not the same... are they? You are looking same Product Codes but the OpportunityLineITem are probably not the same. Your logic should be different I think. You should use the OpportunityLineItem Id as key value in your map and Product Code as values. – Carlos Naranjo Dec 5 '18 at 10:17
  • i should avoid duplicate product code in opportunityline items – user62552 Dec 5 '18 at 10:26
1

The OpportunityLineItems are not the same... are they?

You are looking same Product Codes but the OpportunityLineITem are probably not the same. Your logic should be different I think. You should use the OpportunityLineItem Id as key value in your map and Product Code as values.

Try to invert them and change the map. Something in this line.

Map<Id, String> mapNameWiseoppo = new Map<Id, String>(); 

for(OpportunityLineItem nam : oppo)
        {
            mapNameWiseoppo.put(nam.Id, nam.ProductCode);
        }

It think it is better to use the Id in your map and then use the containsKey() to look for the ProducCode.

As mentioned in my last commend, this is a before insert only, so update DML operations will not fire this trigger. If you want to catch updated records change your trigger declaration to:

trigger duplicatecode on OpportunityLineItem (before insert. before update)

Try this:

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

 Map<String, OpportunityLineItem > OppLineItemMap = 
              new Map<String, OpportunityLineItem >();

for (OpportunityLineItem oppLineItem: System.Trigger.new)
{

// We want to target those new OpportunityLineItems with a Product Code and 
// compare them with OpportunityLineItem already in the system

if ((oppLineItem.ProductCode !=null) &&
       (System.Trigger.isInsert ||
         (oppLineItem.ProductCode != 
           System.Trigger.oldMap.get(oppLineItem.Id).ProductCode )))
{

//Firing error if we find a OpportunityLineItem already with same Product Code 

if (OppLineItemMap.containsKey(oppLineItem.ProductCode))
{
    oppLineItem.ProductCode.addError('code already Exist.');
    //If not found, we add the lead to our leadMap
    }else{
      OppLineItemMap.put(oppLineItem.ProductCode, oppLineItem);
    }
  }
}
//We query our OpportunityLineItem and use the ProductCode as the 
// reference. If found on our map, throw an error 
   for (OpportunityLineItem oppLiIt: 
            [SELECT ProductCode 
                 FROM OpportunityLineItem
                     WHERE ProductCode IN :OppLineItemMap.KeySet()])
 {

  OpportunityLineItem newOpopLiIT = OppLineItemMap.get(oppLiIt.ProductCode);

 // Error needs to be on the field that is causing it, in this case,
 // the ProductCode  field 

   newOpopLiIT.ProductCode.addError('An Opportunity Line Item with this 
     Product Code already exists.');
  }
}

Didn't tested but something like this should work... You can see a similar approach in my bloq:

******** UPDATE ************

Try on the Product2 object and not on the OpportunityLineItem. Create a trigger on the Product2 standard object and paste this.

trigger duplicatecodeProduct on Product2  (before insert, before update) 
{

 Map<String, Product2 > Product2Map = 
              new Map<String, Product2 >();

for (Product2 Product2Item: System.Trigger.new)
{

// We want to target those new Product2 with a Product Code and 
// compare them with Product2 already in the system

if ((Product2Item.ProductCode !=null) &&
       (System.Trigger.isInsert ||
         (Product2Item.ProductCode != 
           System.Trigger.oldMap.get(Product2Item.Id).ProductCode )))
{

//Firing error if we find a Product2 already with same Product Code 

if (Product2Map.containsKey(Product2Item.ProductCode))
{
    Product2Item.ProductCode.addError('code already Exist.');
    //If not found, we add the lead to our leadMap
    }else{
      Product2Map.put(Product2Item.ProductCode, Product2Item);
    }
  }
}
//We query our Product2 and use the ProductCode as the 
// reference. If found on our map, throw an error 
   for (Product2 Product2LiIt: 
            [SELECT ProductCode 
                 FROM Product2
                     WHERE ProductCode IN :Product2Map.KeySet()])
 {

  Product2 newProduct2 = Product2Map.get(Product2LiIt.ProductCode);

 // Error needs to be on the field that is causing it, in this case,
 // the ProductCode  field 

   newProduct2.ProductCode.addError('An Opportunity Line Item with this Product Code already exists.');
  }
}

Tested on the Product2 object and it works:

Trying to update a record that already have the ProductCode throws error:

enter image description here

enter image description here

|improve this answer|||||
  • product code should not save with duplicate value – user62552 Dec 5 '18 at 10:36
  • Yes, that is the idea. But you can use the OpportunityLineItem as the key and in there is any OpportunityLineITem with the same Product Code the error should fired. Your trigger is on before insert only, so make sure that you are aware of the fact that updating a record will not fire this trigger. – Carlos Naranjo Dec 5 '18 at 10:50
  • can u help with the code – user62552 Dec 5 '18 at 10:52
  • @raj try the code on my answer. – Carlos Naranjo Dec 5 '18 at 11:25
  • productcode in if statement is showing error – user62552 Dec 5 '18 at 11:31

Your Answer

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