@RestResource(urlMapping='/CreateOrder/*')
global with sharing class RestAPiOrderCreation {
@HttpPost
global static WrapperClass createOrderOrderItemRestApi() {
List<OrderItem> orderItemsTempList = new List<OrderItem>();
WrapperClass responseWrapper = new WrapperClass();
MAP<String,integer> lineNumberQtyMap = new MAP<String,integer>();
Set<String> lineItemNumberSet = new Set<String>();
List<Order> orderList = new List<Order>();
String orderType;
List<PricebookEntry> pricebookEntryList = new List<PricebookEntry>();
Date effectiveDate;
String errorMessage;
try{
Map<String, Object> params = (Map<String, Object>)JSON.deserializeUntyped(RestContext.request.requestBody.toString());
List<object> orderMainlist = (List<Object>)params.get('order');
for(object obj :orderMainlist){
Map<String, Object> params2 = (Map<String, Object>)obj;
order ordInst = new order();
String effDateStr = (String) params2.get('EffectiveDate'); // **
ordInst.EffectiveDate = Date.valueOf(effDateStr);
effectiveDate = Date.valueOf(effDateStr);
String deliveryDateStr = (String) params2.get('DeliveryDate');
ordInst.PoNumber = (String) params2.get('PONumber');
ordInst.Status = 'Draft';
orderList.add(ordInst);
List<Object> orderItemsvar = (List<Object>) params2.get('OrderItems');
for(object objOI : orderItemsvar){
Map<String, Object> params3 = (Map<String, Object>)objOI;
OrderItem ordItemInst = new OrderItem();
ordItemInst.Quantity =Integer.valueOf( params3.get('Qty'));
lineNumberQtyMap.put(String.valueOf(params3.get('LineNumber')),Integer.valueOf( params3.get('Qty')));
orderItemsTempList.add(ordItemInst);
lineItemNumberSet.add(String.valueOf( params3.get('LineNumber')));
}
}
pricebookEntryList = [SELECT id,name,Product2Id,Product2.name,Product2.StockKeepingUnit,UnitPrice, Pricebook2Id, Pricebook2.name
FROM PricebookEntry
WHERE
Product2.StockKeepingUnit IN : lineItemNumberSet];
orderList[0].Pricebook2ID = pricebookEntryList[0].Pricebook2Id;
orderList[0].status = 'Draft';
orderList[0].accountid ='0018d00000fkDsnAAE';
orderList[0].ContractId ='8008d000000ExAFAA0';
orderList[0].EffectiveDate =system.today()+10;
system.debug('===Before orderList=='+orderList);
if(!orderList.isEmpty()) {
if(Schema.sObjectType.Order.isCreateable()) {
insert orderList;
}
}
system.debug('===orderList=='+orderList);
pricebookEntryList = [SELECT id,name,Product2Id,Product2.name,Product2.StockKeepingUnit,UnitPrice, Pricebook2Id, Pricebook2.name
FROM PricebookEntry
WHERE
Product2.StockKeepingUnit IN : lineItemNumberSet AND Pricebook2Id =: orderList[0].Pricebook2Id ];
Map<String,PricebookEntry> pbeMap = new Map<String,PricebookEntry>();
List<OrderItem> orderItemsList = new List<OrderItem>();
for(PricebookEntry pbeInst: pricebookEntryList){
pbeMap.put(pbeInst.Product2.StockKeepingUnit,pbeInst);
OrderItem instobj = new OrderItem();
if(lineNumberQtyMap.containsKey((pbeInst.Product2.StockKeepingUnit)) ){
lineNumberQtyMap.get(pbeInst.Product2.StockKeepingUnit);
instobj.OrderId = orderList[0].id;
instobj.Quantity = lineNumberQtyMap.get(pbeInst.Product2.StockKeepingUnit);
instobj.Product2Id = pbeInst.Product2id;
instobj.pricebookentryId = pbeInst.id;
instobj.UnitPrice = pbeInst.UnitPrice;
orderItemsList.add(instobj);
}
}
system.debug('===orderItemsList=='+orderItemsList);
if(Schema.sObjectType.OrderItem.isCreateable()) {
insert orderItemsList;
}
}
catch (Exception e){
errorMessage= e.getMessage() +'--getCause'+e.getCause()+'==getInaccessibleFields=='+e.getInaccessibleFields()+'-getLineNumber-'+e.getLineNumber()+'-getStackTraceString-'+e.getStackTraceString();
responseWrapper.status ='409';
responseWrapper.message =e.getMessage();
responseWrapper.sfOrderID ='';
}
if(!orderList.isEmpty() && String.isEmpty(errorMessage)){
responseWrapper.status ='200';
responseWrapper.message ='success';
responseWrapper.sfOrderID =String.valueof(orderList[0].id);
}
else{
responseWrapper.status ='409';
responseWrapper.message =errorMessage;
}
return responseWrapper;
}
// Wrapper WrapperClass for return structure
global class WrapperClass{
Public String status {get;set;}
Public String message {get;set;}
Public String sfOrderID {get;set;}
}
}
==========================