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
si risi ri 

Trigger to restrict duplicate product items

Hi,
I have a requirement that from Opportunity (i.e. create Order and Order Line Items from Opportunity, whenever product is added in Opportunityline item product should be created in orderItem and whenever product is deleted in Opportunityline item product should be deleted in orderItem). As per my code order and orderitems were creating but when ever I edit the Opportunity orderitems are creating duplicate times.
public class example {
    
    public static void CreatingOrders(List<Opportunity> newlist){
        set<Id> oppId = new set<Id>();
        for(Opportunity opp : newList){
            oppId.add(opp.id);    
        }
        List<Opportunity> oppList = [select id,name,StageName,Contract_number__c,ContractId,Accountid,Pricebook2Id, (select Status,Account.name,enddate from Orders)
                                     from Opportunity where Id =: oppId];
          
        List<Order> insertorders = new List<Order>();
        
        for(Opportunity opp1 : oppList){
            
            if (opp1.Orders.size()>0){
                
            } 
            else if(opp1.StageName == 'Closed-Won') {
                Order ordr =new Order();
                ordr.AccountId = opp1.AccountId;
                ordr.OpportunityId = opp1.id;
                ordr.Status = 'Draft';
                ordr.EffectiveDate = system.today();
                ordr.Pricebook2Id = opp1.Pricebook2Id;
          
              ordr.ContractId = opp1.ContractId; 
                insertorders.add(ordr);
            }
        }
        insert insertorders;
       
    }
    
    public static void CreatingOrderlineitems(List<Opportunity> newlist1)
    {
        set<Id> oppId = new set<Id>();
        for(Opportunity opp : newList1){
            oppId.add(opp.id);
        }
       
        
        List<Order> ordrList  = [select Status,Account.name,enddate,Pricebook2Id,OpportunityId,Opportunity.name,Opportunity.stagename,
                                 (select UnitPrice,PricebookEntryId,Description,Quantity, Discount_Percentage__c, One_Time__c,
                                   Integration_Key_Product__c from OrderItems) from Order where Opportunityid =: oppId];
        

        Set<Id> opportunityIds = new Set<Id>();
        
        List<Order> orderList = new List<Order>();
        
        for(Order o : ordrList)
        {
            if(o.Opportunityid != NULL)
            {
                opportunityIds.add(o.Opportunityid);
                orderList.add(o);
            }
        }
        
        Map<Id, Opportunity> oppsWithLineItems = new Map<Id, Opportunity>([SELECT Id, Pricebook2Id, (SELECT Description,Id,ListPrice,Name,OpportunityId,
                                                                                                     Product2Id,ProductCode,Quantity,TotalPrice,UnitPrice,PricebookEntryId,
                                                                                                     Discount_Percentage__c, One_time_applicable_formula__c,Integration_Key_Product__c 
                                                                                                     FROM OpportunityLineItems) from Opportunity WHERE Id IN :opportunityIds]); 
        if(opportunityIds.size() > 0)
        
        {
            
            List<OrderItem> InsertorderItems = new List<OrderItem>();
            List<Order>  upordrList = new List<Order>();
            for(Order o : ordrList)
            {
                
                for(OrderItem ordItem : o.OrderItems){

                Opportunity oppWithLineItem = oppsWithLineItems.get(o.Opportunityid);
                
                for(OpportunityLineItem oli : oppWithLineItem.OpportunityLineItems)
                {
                    
                    InsertorderItems.add(new OrderItem(OrderId=o.Id,UnitPrice=oli.UnitPrice,PricebookEntryId = oli.PricebookEntryId,Description = oli.Description,
                                                          Quantity =oli.Quantity, Discount_Percentage__c = oli.Discount_Percentage__c,
                                                             One_Time__c  = oli.One_time_applicable_formula__c));
                
                }
            }
           }
              insert InsertorderItems;
        }
    }
}
vignesh Kumar 35vignesh Kumar 35

That code only only works for creating new records, that's the reason for duplicate record cretaion. Please write the code for updating the records like below.
 

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_examples_insert_update.htm