0

I have to write a duplication trigger on OpportunityLineItem to perform a duplication check so that the same product can not be added to the Opportunity again.If the same product is added to the opportunity again, it should throw an error on the UI saying that "This product already exist on the opportunity."I have written the code .So far even if a new product is added it still shows an error message.

 trigger OpportunityLineItemCheck on OpportunityLineItem (after insert,after update) {
OpportunityLineItemCheckOperations opportunityLineItemCheckOperations=new OpportunityLineItemCheckOperations();
opportunityLineItemCheckOperations.checkOnOpportunityLineItems(trigger.new);
 }



public class OpportunityLineItemCheckOperations {
public  void checkOnOpportunityLineItems(List<OpportunityLineItem> opportunityProductList){
    List<Opportunity> opportunityList=[SELECT ID FROM Opportunity];
    system.debug('opportunityList@@@@@'+opportunityList);
    OpportunityLineItem opportunityPrductObj=[SELECT ID,OpportunityId FROM OpportunityLineItem WHERE OpportunityId IN:opportunityList ];
    system.debug('opportunityPrductObj@@@@@'+opportunityPrductObj);
    for(OpportunityLineItem oli:opportunityProductList ){
        if (oli.OpportunityId==opportunityPrductObj.OpportunityId) {
            oli.addError('Opportunity Product Already Exists');
        }
    }
}
}
4

No need to do a trigger at all.

  1. Add a field on the OLI name "Unique Check" or whatever and make it unique
  2. WFR to populate the field with a concat of the Opportunity Id and the Product2Id

Whenever a user attempts to add the same product to a given opportunity they will get a duplicate error message

But if you must (debugging may be required)

trigger OpportunityLineItemCheck on OpportunityLineItem (before insert,before update) {
    opportunityLineItemCheckOperations.checkOnOpportunityLineItems(trigger.new);
 }



public class OpportunityLineItemCheckOperations {

    public static void checkOnOpportunityLineItems(List<OpportunityLineItem> opportunityProductList){
        Set<Id> oppIds = New Set<Id>(); //Holds OppIds in the Line Items List
        Map<Id,Id> oppToNewProd = new Map<Id,Id>(); //Opp to products in the line item list

        //Populate collections
        for(OpportunityLineItem oli : opportunityProductList){
            oppIds.add(oli.OpportunityId);
            oppToNewProd.put(oli.OpportunityId,oli.Product2Id);
        }

        //Get Map of opportunity to line items NOT in the trigger context
        Map<Id,Opportunity> oppWithLineItems = New Map<Id,Opportunity>(
            [Select Id, (Select Id, product2Id From OpportunityLineItems Where Id Not In :opportunityProductList) From Opportunity Where Id In :oppIds] 
        );

        for(OpportunityLineItem oli : opportunityProductList){ //For each line item in context
            Opportunity tmp = oppWithLineItems.get(oli.OpportunityId); //Get the Opportunity

            for(OpportunityLineItem oli : tmp.OpportunityLineItems){ //See if a line item in context has a product related to the Opp already
                if(oppToNewProd.containsKey(oli.OpportunityId) && oppToNewProd.get(oli.OpportunityId).contains(oli.Product2Id)){
                    oli.addError('Opportunity Product Already Exists');
                }
            }           

        }
    }
}
| improve this answer | |
  • Actually my requirement is to achieve this functionality with the help of a trigger.I cannot add fields for that – Saswata Das May 4 '17 at 17:49
  • 2
    No bad requirement is set in stone. If there is a better way to do it then that should be pursued. But if you must answer has been updated. – Eric May 4 '17 at 18:07

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.