• vignesh Kumar 35
  • NEWBIE
  • 0 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 2
    Replies
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;
        }
    }
}
  • July 09, 2019
  • Like
  • 0
Hi, 

Am trying to change ownersship on leads and am getting the following error, I got in touch with MC to check if they could help me but no luck so thought I would check and see if anyone could educate me and solve this issue.

Error: Apex trigger MC4SF.Lead caused an unexpected exception, contact your administrator: MC4SF.Lead: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id a0O1500000MiJq5EAF; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []: (MC4SF)