+ Start a Discussion
Board salesforceBoard salesforce 

Trigger to restrict duplicate opportunitylineitems

Hi Techie's,


Can any one help out with a trigger that restricting dupicate opportunitylineitems 

Thanks,
Ram.
Best Answer chosen by Board salesforce
SalesFORCE_enFORCErSalesFORCE_enFORCEr
Fixed it
trigger OppotunitylineitemDuplicateCheckTrigger on OpportunityLineItem (before insert) {
    Map<Id,List<Id>> mapProduct = new Map<Id,List<Id>>();
    Set<Id> setOppIds = new Set<Id>();
    for(OpportunityLineItem oli: Trigger.new){
        setOppIds.add(oli.OpportunityId);
    }
    for(OpportunityLineItem oli: [Select Id, Product2Id, OpportunityId from OpportunityLineItem where OpportunityId IN:setOppIds]){
        if(mapProduct.containsKey(oli.OpportunityId))
            mapProduct.get(oli.OpportunityId).add(oli.Product2Id);
        else
            mapProduct.put(oli.OpportunityId,new List<Id>{oli.Product2Id});
    }
    for(OpportunityLineItem oli: Trigger.new){
        if(mapProduct.containsKey(oli.OpportunityId)){
            for(Id prodId: mapProduct.get(oli.OpportunityId)){
                if(oli.Product2Id.equals(prodId))
                    oli.addError('Please select another product');
            }
        }
    }
}

All Answers

Narveer SinghNarveer Singh
Hi Ram,

You can use below options to avoid duplicacy :

1) Make an external field on Oppotunity Line Item,it will protect from duplicate entires.
2) Make a set of Line Item Ids as Set<Id> ids = new Set<Id>(); 
Since Insert or updated operation does not works on set you need to convert this into List and the complete your operation with List.

​Hope this helps you!

Please accept my solution as Best Answer if my reply was helpful. It will make it available for other as the proper solution.Let me know if anything else.

Best Regards
Narveer
Board salesforceBoard salesforce
trigger OppotunitylineitemDuplicateCheckTrigger on Opportunitylineitem (before insert) {
    
    
    try{
    Set<Id> pbeIds = new Set<Id>();
    
    for (OpportunityLineItem opli : Trigger.new) {
        pbeIds.add(opli.PricebookEntryId);
        system.debug('pbeIds'+pbeIds);
    }
    Map<Id, PricebookEntry> pbeMap = new Map<Id, PricebookEntry>([select Id, Product2.Name from PricebookEntry where Id in :pbeIds]);
    system.debug('pbeMap'+pbeMap);
    Map<id,Opportunitylineitem> oppList = new Map<id,Opportunitylineitem>(
            [Select id,Name,Product2.Name,PricebookEntryId,Opportunityid from OpportunityLineItem]);
    system.debug('Opplist is '+oppList);
    for(Opportunitylineitem opli: trigger.New){
       if(pbeMap.get(opli.PricebookEntryId).Product2.Name==oppList.get(opli.PricebookEntryId).Product2.Name){
           system.debug('oppList.get(opli.PricebookEntryId).Product2.Name is --'+oppList.get(opli.PricebookEntryId).Product2.Name );
           system.debug('pbeMap.get(opli.PricebookEntryId).Product2.Name is '+pbeMap.get(opli.PricebookEntryId).Product2.Name);
            opli.addError('Opportunity Product already exists in your Organization with name ');
            }
            
        }
        
        
    }catch(exception e){
        system.debug('exception is '+e.getLineNumber());
    }
    }
Thanks for the respond,
Can you help in this 
SalesFORCE_enFORCErSalesFORCE_enFORCEr
Try this
trigger OppotunitylineitemDuplicateCheckTrigger on OpportunityLineItem (before insert) {
    Map<Id,List<Id>> mapProduct = new Map<Id,List<Id>>();
    Set<Id> setOppIds = new Set<Id>();
    for(OpportunityLineItem oli: Trigger.new){
        setOppIds.add(oli.OpportunityId);
    }
    for(OpportunityLineItem oli: [Select Id, Product2Id, OpportunityId from OpportunityLineItem where OpportunityId IN:setOppIds]){
        if(mapProduct.containsKey(oli.OpportunityId))
            mapProduct.get(oli.OpportunityId).add(oli.Product2Id);
        else
            mapProduct.put(oli.OpportunityId,new List<Id>{oli.Product2Id});
    }
    for(OpportunityLineItem oli: Trigger.new){
        for(Id prodId: mapProduct.get(oli.OpportunityId)){
            if(oli.Product2Id.equals(prodId))
                oli.addError('Please select another product');
        }
    }
}
Board salesforceBoard salesforce
Hi Shalabh,

getting below error  while i insert a new opportunitylineitem

Apex trigger OppotunitylineitemDuplicateCheckTrigger caused an unexpected exception, contact your administrator: OppotunitylineitemDuplicateCheckTrigger: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.OppotunitylineitemDuplicateCheckTrigger: line 14, column 1  
SalesFORCE_enFORCErSalesFORCE_enFORCEr
Fixed it
trigger OppotunitylineitemDuplicateCheckTrigger on OpportunityLineItem (before insert) {
    Map<Id,List<Id>> mapProduct = new Map<Id,List<Id>>();
    Set<Id> setOppIds = new Set<Id>();
    for(OpportunityLineItem oli: Trigger.new){
        setOppIds.add(oli.OpportunityId);
    }
    for(OpportunityLineItem oli: [Select Id, Product2Id, OpportunityId from OpportunityLineItem where OpportunityId IN:setOppIds]){
        if(mapProduct.containsKey(oli.OpportunityId))
            mapProduct.get(oli.OpportunityId).add(oli.Product2Id);
        else
            mapProduct.put(oli.OpportunityId,new List<Id>{oli.Product2Id});
    }
    for(OpportunityLineItem oli: Trigger.new){
        if(mapProduct.containsKey(oli.OpportunityId)){
            for(Id prodId: mapProduct.get(oli.OpportunityId)){
                if(oli.Product2Id.equals(prodId))
                    oli.addError('Please select another product');
            }
        }
    }
}
This was selected as the best answer
Board salesforceBoard salesforce
Thanks,
it works like charm.