function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Kumar GKumar G 

How to skip trigger firing for clone functionality.

Hi Everyone,

I have created before trigger to restrict duplicate rcords in opportunity lineitems but while cloning opportunity with opportunity lineitems then created trigger throughing errors like : A OpportunityLineItem with this ProductCode already exists(Plz find the screen short)                                                         

How to skip trigger fire for cloning functionality , please suggest me the solution.


User-added image
 
Wilfredo Morillo 20Wilfredo Morillo 20
What's the trigger code?
Usman Yousaf 15Usman Yousaf 15
It seems like Product code is a unique key, it won't let you clone.
Kumar GKumar G
Thanks for your response , please find the trigger code below :
trigger OliDuplicatePreventer on OpportunityLineItem (before insert, before update) 
{
    Map<String, OpportunityLineItem> Olimap = new Map<String, OpportunityLineItem>();
        for (OpportunityLineItem OpportunityLineItem : System.Trigger.new) 
        {        
            if ((OpportunityLineItem.ProductCode != null) &&
            (System.Trigger.isInsert ||
                (OpportunityLineItem.ProductCode != System.Trigger.oldMap.get(OpportunityLineItem.Id).ProductCode))) 
                { 
                    if (Olimap.containsKey(OpportunityLineItem.ProductCode)) 
                    {
                    OpportunityLineItem.ProductCode.addError('Another new OpportunityLineItem has the '
                    + 'same ProductCode address.');
                    } else {
                    Olimap.put(OpportunityLineItem.ProductCode, OpportunityLineItem);
                    }
                }
            }
            for (OpportunityLineItem OpportunityLineItem : [SELECT ProductCode FROM OpportunityLineItem WHERE ProductCode IN :Olimap.KeySet()]) 
            {
                    OpportunityLineItem newOli = Olimap.get(OpportunityLineItem.ProductCode);
                    newOli.ProductCode.addError('A OpportunityLineItem with this ProductCode '+ ' already exists.');
            }
}