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 

execution of AfterUpdate caused by: System.TypeException: Invalid id value for this SObject type:


I am trying to create orderitems from OpportunityLine items and also creating order on Opportunity. But I am getting the following error
Error: execution of AfterUpdate caused by: System.TypeException: Invalid id value for this SObject type: 00kq0000008mWXjAAM: Class.Autocreation .CreatingOrders:
at this particular line [ordr1.Id = oli.Id;]
Which Id needs to be take, I don't understand Can anyone help me.Thanks inadvance

public static void CreatingOrders(List<Opportunity> newlist){
      set<Id> oppId = new set<Id>();
    for(Opportunity opp : newList){
        oppId.add(opp.id); //Adding Opportunity's to Set.    
         }
    List<Opportunity> oppList = [select id,name,StageName,Accountid, (select Status,Account.name,enddate from Orders)
                                 from Opportunity where Id =: oppId];

     List<OpportunityLineItem> oppList1 = [SELECT id,Quantity,Product2Id,
                                  UnitPrice,Description, TotalPrice,PricebookEntry.Name, PricebookEntry.Product2.Family,
                                  OpportunityId FROM OpportunityLineItem where OpportunityId =: oppId];



    List<Order> insrtordrs = new List<Order>();
    List<OrderItem> insrtordrs1 = new List<OrderItem>();

    for(Opportunity opp1 : oppList){
        for(OpportunityLineItem oli : oppList1 ){  
        if (opp1.Orders.size()>0){
            system.debug('Orders exists*******************' +opp1.Orders);

        } 
        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.ContractId = opp1.ContractId; 
          insrtordrs .add(ordr);

            OrderItem ordr1 =new OrderItem();
            ordr1.Id = oli.Id;
            ordr1.PricebookEntryId = oli.PricebookEntryId;
            ordr1.UnitPrice        = oli.UnitPrice;
            ordr1.Description      = oli.Description;
            ordr1.Quantity         = oli.Quantity;
            insrtordrs1 .add(ordr1);  

        }   
       }
    }
    insert insrtordrs ;
    insert insrtordrs1 ;

 }
Best Answer chosen by si ri
Arun ParmarArun Parmar
Hi Si ri
Remove this line which i commented.
           
OrderItem ordr1 =new OrderItem();
            //ordr1.Id = oli.Id;  // remove this line or comment it.
            ordr1.PricebookEntryId = oli.PricebookEntryId;
            ordr1.UnitPrice        = oli.UnitPrice;
            ordr1.Description      = oli.Description;
            ordr1.Quantity         = oli.Quantity;
            insrtordrs1 .add(ordr1);  

All Answers

Meghna Vijay 7Meghna Vijay 7
Hi Si ri,

While performing Insert operation on an object there is no need to set it's Id unless you want to perform Update operation and you are setting it's Id with the Opportunity Line Item Id which is not correct. Please remove this line " ordr1.Id = oli.Id;".

Thanks

 
Arun ParmarArun Parmar
Hi Si ri,

You are doing everything fine but only one mistake you have commited, you are mentioning ordr1.Id = oli.Id; which is not reuired while inserts (do not mention record id).
remove this line and your code will work properly.

Thanks
 
si risi ri
Hi Arun Parmar!
It's not working, I have tried  what you have mentioned.
Arun ParmarArun Parmar
Hi Si ri
Remove this line which i commented.
           
OrderItem ordr1 =new OrderItem();
            //ordr1.Id = oli.Id;  // remove this line or comment it.
            ordr1.PricebookEntryId = oli.PricebookEntryId;
            ordr1.UnitPrice        = oli.UnitPrice;
            ordr1.Description      = oli.Description;
            ordr1.Quantity         = oli.Quantity;
            insrtordrs1 .add(ordr1);  
This was selected as the best answer
Piyush Gautam 6Piyush Gautam 6
 Hi Si ri,  

Please try this code.

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 public static void CreatingOrders(List<Opportunity> newlist){
      set<Id> oppId = new set<Id>();
    
    for(Opportunity opp : newList){
        oppId.add(opp.id); //Adding Opportunity's to Set.    
         }
    
    List<Opportunity> oppList = [select id,name,StageName,Accountid, (select Status,Account.name,enddate from Orders)
                                 from Opportunity where Id =: oppId];

     List<OpportunityLineItem> oppList1 = [SELECT id,Quantity,Product2Id,
                                  UnitPrice,Description, TotalPrice,PricebookEntry.Name, PricebookEntry.Product2.Family,
                                  OpportunityId FROM OpportunityLineItem where OpportunityId =: oppId];



    List<Order> insrtordrs = new List<Order>();
    List<OrderItem> insrtordrs1 = new List<OrderItem>();
    
    
    for(Opportunity opp1 : oppList){
        
        if (opp1.Orders.size()>0){
            system.debug('Orders exists*******************' +opp1.Orders);
        } 
        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.ContractId = opp1.ContractId; 
          insrtordrs .add(ordr);
        }
    }
    
    if(insrtordrs.size()>0){
        insert insrtordrs ;
    }
    
    map<id, Opportunity> oppOrderMap= new Map<id, Opportunity>([SELECT Id , (select id from Orders) from Opportunity]);
          
    
    

    for(Opportunity opp1 : oppList){
        
        if (opp1.Orders.size()>0){
            system.debug('Orders exists*******************' +opp1.Orders);
        } 
        else if(opp1.StageName == 'Closed-Won') {
            
        for(OpportunityLineItem oli : oppList1 ){  
            if(opp1.Id== oli.opportunityId) {
        
                    OrderItem ordr1 =new OrderItem();
                
                    string ordId= string.valueOf(oppOrderMap.get(opp1.Id).Orders[0]);
                    ordr1.OrderId = ordId;
                    ordr1.PricebookEntryId = oli.PricebookEntryId;
                    ordr1.UnitPrice        = oli.UnitPrice;
                    ordr1.Description      = oli.Description;
                    ordr1.Quantity         = oli.Quantity;
                    insrtordrs1 .add(ordr1);
                             
                    }   
                }
            
            if(insrtordrs1.size()>0){
            insert insrtordrs1 ;
            }
         } 
        }