-3

How do I create an order with orderitems. The following code gives me error - Error CreateSummaryOrder Method does not exist or incorrect signature: void add(OrderItem) from the type Order (67:18)

I also can not assign like this //newI.orderid = OrderID; (commented out below) I get error - "Field is not writeable: OrderItem.OrderId"

The following code successfully creates an order and orderitems but I need to link them together. thank you

public with sharing class CreateSummaryOrder {
    public CreateSummaryOrder() {

    }

    @AuraEnabled
    public static String CreateOrder(List <Map<String, String>> orderJSON, String accountId){

        Account dPData = [SELECT aforza__Default_Price_Book__c,BillingPostalCode,BillingCity,
        BillingState,BillingStateCode,BillingStreet,Depot__c,NextDeliveryDate__c,
        ShippingAddress,ShippingCity,ShippingCountry,ShippingCountryCode,ShippingStateCode,
        ShippingStreet,StoreName__c, Tam_Owner_Code__c, DeliveryPointReference__c,ShippingPostalCode FROM Account where id =: accountId];
       // system.debug(JSON.stringify(orderJson));
       system.debug('hello');
       system.debug(orderjson.size());

       Order newOrder = new Order();
       newOrder.put('Name', 'Test');
       newOrder.status = 'Draft';
       newOrder.RecordTypeId = RecordTypes.ORDER_TELESALES_ID;
       newOrder.accountId = accountId;
       newOrder.EffectiveDate = Date.today();
       newOrder.Delivery_Date__c = dPData.NextDeliveryDate__c;
       newOrder.Depot__c = dPData.Depot__c;
       newOrder.Tam_Owner_Code__c = dpData.Tam_Owner_Code__c;
       //??newOrder.owningTam = tam on dp record;
       /////////OwnerId/createdby = personSelected;
       newOrder.Default_Price_Book__c = dPData.aforza__Default_Price_Book__c;
       //newOrder.default_Inventory__c = inventory form depot
        newOrder.DP_Name__c = dPData.StoreName__c;
        newOrder.DP_Reference__c = dpData.DeliveryPointReference__c;

        newOrder.BillingCity = dpData.BillingCity;
        newOrder.BillingPostalCode = dpData.BillingPostalCode;
        newOrder.BillingState = dpData.BillingState;
        newOrder.BillingStreet = dpData.BillingStreet;
        newOrder.ShippingCity = dpData.ShippingCity;
        newOrder.ShippingCountry = dpData.ShippingCountry;
        newOrder.ShippingPostalCode = dpData.ShippingPostalCode;
        newOrder.ShippingStreet = dpData.ShippingStreet;


Id OrderId;

Database.SaveResult sr = Database.insert(newOrder);

    OrderId = sr.getId();
   

System.debug(OrderId);
       list<OrderItem> orderItems = new list<OrderItem>();

       for(Map<String,String> maps :orderJson){
        OrderItem newI = new OrderItem();
    
       for(String k:maps.keyset()){
        newI.quantity = Decimal.valueof(maps.get('Average'));
        newI.Id = maps.get('Id');
        //newI.orderid = OrderID;
        
     orderItems.add(newI);
     
       }
       
       }     
       for(OrderItem ord: orderItems){
        newOrder.add(ord);
       } 
       system.debug(orderitems);  
       system.debug(newOrder);

       List<OrderItem> os = [select id from orderitem where orderid =:OrderId];
       system.debug(os);
      
       return OrderId; 
      
    } }
4
  • 3
    Please edit your question to give us the exact and full text of the error message. When you paraphrase it, you're leaving out important information. Also, do take care to format your code appropriately and indent consistently. The easiest way to do that is to highlight your code in the editor and either press ctrl + k, or press the {} button.
    – Derek F
    Commented Oct 3, 2022 at 18:25
  • Ive edited my question
    – knay
    Commented Oct 3, 2022 at 20:15
  • If you're creating new OrderItem records you should not be assigning a value to the ID field at all; newI.Id = maps.get('Id'); is wrong. In general, when troubleshooting you should start with the simplest code possible, e.g. create a very basic Order record and then create a very basic OrderItem. Once that's working, you can add more complexity. Commented Oct 3, 2022 at 23:44
  • Also once you insert newOrder, you can reference the ID directly by newOrder.Id, you don't need to extract the ID into a separate variable. Commented Oct 3, 2022 at 23:46

1 Answer 1

1

Error CreateSummaryOrder Method does not exist or incorrect signature: void add(OrderItem) from the type Order (67:18)

Comes from the fact that your newOrder variable is an Order object, then at some point you try

newOrder.add(ord)

(where ord is an OrderItem).

Simply put, you can't do that - newOrder is not a List, and that is now how you associate records in Salesforce.


If you look in the Object Manager at the fields on the Order object, you will not see anything related to an OrderItem (a.k.a, Order Product). So no matter how you try to cram them in there, you cannot add the OrderItems onto the Order.

But if you look at the OrderItem fields, you will find OrderId which is a Lookup to the Order object.

Steps
  1. Create and insert your Order

    As David Cheng stated in a comment under your question, whenever you insert an object in Apex you do not need to then query for the new object's Id - it is "automagically" added to your object variable.

    Example

    Order newOrder = new Order();
    newOrder.Name = 'Test';
    // Populate at least all required fields
    ...
    insert newOrder; // newOrder.Id now contains the Id of the newly inserted Order
    
  2. Create your OrderItem(s)

    Be sure to set the orderId of this/these Order Product(s) to be the Id of the Order you created earlier.

    List<OrderItem> list_OrderItems = new List<OrderItem>();
    for (criterion : criteria) {
       OrderItem newOrderItem = new OrderItem();
       newOrderItem.orderId = newOrder.Id;
       // Populate at least all required fields
       ...
       list_OrderItems.add(newOrderItem);
    }
    insert list_OrderItems;
    

Also as David Cheng stated, start with the simplest, most basic, attempt at this - sort of a Proof of Concept - to make sure you can get the basic mechanics to work. Start with required fields only, and with only one OrderItem. Once you have an Order and an OrderItem inserted & connected, then you can start making it more complex.

NOTE: Some of those required fields could be references to other objects that you may need to query for or create (e.g., Account, SalesChannel, Pricebook2, Product2, OrderDeliveryGroup, PricebookEntry).

Not the answer you're looking for? Browse other questions tagged .