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;
} }
ctrl
+k
, or press the{}
button.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.newOrder
, you can reference the ID directly bynewOrder.Id
, you don't need to extract the ID into a separate variable.