- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-21-2017 09:53 AM
Asking the community for assistance on this one.
For this integration to a 3rd party provider of computer equipment, I need to structure an
outbound POST REST message using nested objects and arrays (see the required JSON structure below).
I've begun writing some objects that will fall into mainObj, but the nesting and structuring is where
I could use some advice. Thank you in advance.
var orderCreateRequest = {}; // order request object
orderCreateRequest.ClientID = "" + '001-clientID';
orderCreateRequest.ClientTransactionID = "" + current.number;
var OrderHeader = {}; // order header object
OrderHeader.CustomerOrderNumber = "" + current.number;
OrderHeader.SoldToNumber = ""+ '10314198';
//
var mainObj = {};
mainObj.MT_WEBOrderCreateRequest = orderCreateRequest;
var r = new sn_ws.RESTMessageV2('Insight Sales Order Processing', 'post');
//override authentication profile
var authentication_type = 'oauth2';
var profile_name = 'OAuth default_profile';
r.setAuthentication(authentication_type, profile_name);
//
r.setRequestBody(body);
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
Required JSON structure for Purchase Order =========
{
"MWOrderCreateRequest": {
"OrderCreateRequest": {
"ClientID": "CHAR32-Required",
"ClientTransactionID": "CHAR32-Required",
"SalesOrderCreate": [{
"OrderHeader": {
"CustomerOrderNumber": "CHAR35-Required",
"CustomerOrderDate": "YYYYMMDD-Optional",
"ReleaseNumber": "CHAR35-Optional",
"SoldToNumber": "CHAR10-Required",
"ContactPerson": "CHAR10-Optional",
"CurrencyCode": "CHAR3-Optional",
"RequestDeliveryDate": "YYYYMMDD-Optional",
"ShippingMethod": "CHAR2-Optional",
"ShippingCarrier": "CHAR10-Optional",
"CarrierAccount": "CHAR10-Optional",
"ASNEmail": "CHAR255-Optional",
"SpecialProcIndicator": "CHAR4-Optional",
"TextSpecialProcIndicator": "CHAR200-Optional",
"TextSpecialInstructionsHold": "CHAR200-Optional",
"BusinessPartnerHeader": [{
"PartnerFunction": "CHAR2-Optional",
"PartnerNumber": "CHAR10-Optional",
"Name": "CHAR40-Optional",
"Name2": "CHAR40-Optional",
"Attention": "CHAR40-Optional",
"Street": "CHAR60-Optional",
"Street2": "CHAR40-Optional",
"City": "CHAR35-Optional",
"PostalCode": "CHAR10-Optional",
"Region": "CHAR3-Optional",
"CountryCode": "CHAR3-Optional"
}],
"SmartTrackerHeader": [{
"Key": "CHAR50-Optional",
"Value": "CHAR225-Optional"
}]
},
"LineItems": [{
"MaterialNumber": "CHAR18-Conditional",
"CustMaterialNumber": "CHAR35-Conditional",
"LineNumber": "CHAR6-Optional",
"HighLevelItem": "NUMC6-Optional",
"Quantity": "CHAR10-Required",
"SalesUoM": "CHAR3-Optional",
"CustExpectedPrice": "CURR15-Optional",
"RegistrationContactName": "CHAR30-Optional",
"RegistrationContactPhoneNumber": "CHAR30-Optional",
"RegistrationContactEmail": "CHAR30-Optional",
"RequestDeliveryDate": "YYYYMMDD-Optional",
"AgreementNumber": "CHAR40-Optional",
"Contract": "CHAR10-Optional",
"SmartTrackerItem": [{
"Key": "CHAR50-Optional",
"Value": "CHAR225-Optional"
}]
}]
}]
}
}
}
Solved! Go to Solution.
- Labels:
-
Integrations
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-21-2017 12:54 PM
I would create my structure and push my array items into the higher level arrays like this...
var _master = {
"MWOrderCreateRequest": {
"OrderCreateRequest": {
"ClientID": "CHAR32-Required",
"ClientTransactionID": "CHAR32-Required",
"SalesOrderCreate": []
}
}
};
var _order = {
"OrderHeader": {},
"LineItems": []
};
var _item = {
"MaterialNumber": "CHAR18-Conditional",
"CustMaterialNumber": "CHAR35-Conditional",
"LineNumber": "CHAR6-Optional",
"HighLevelItem": "NUMC6-Optional",
"Quantity": "CHAR10-Required",
"SalesUoM": "CHAR3-Optional",
"CustExpectedPrice": "CURR15-Optional",
"RegistrationContactName": "CHAR30-Optional",
"RegistrationContactPhoneNumber": "CHAR30-Optional",
"RegistrationContactEmail": "CHAR30-Optional",
"RequestDeliveryDate": "YYYYMMDD-Optional",
"AgreementNumber": "CHAR40-Optional",
"Contract": "CHAR10-Optional",
"SmartTrackerItem": []
};
//push multiple items into the line items array
_order.LineItems.push(_item);
//push multiple orders into the SalesOrderCreate array.
_master.MWOrderCreateRequest.OrderCreateRequest.SalesOrderCreate.push(_order);
gs.print(JSON.stringify(_master,null,4));
Or, if you have and items query that returns an array just assign it to the value.
_order.LineItems = glideRecord_Result;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-21-2017 04:45 PM
Jim, I need to dynamically replace (this codes runs in a BR), the value CHAR32-Required with an actual value from a table and have tried using the stringify shown below.
- "OrderCreateRequest": {
- "ClientID": "CHAR32-Required",
JSON.stringify({ ClientID: current.clientID.toString() }),
Can you recommend how best to build this outbound rest message with actual values?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-22-2017 06:18 PM
After more work on this, here's the object, array structure that worked. Jim, I appreciate you putting me on the correct path...and for that, a correct answer.
Below is certainly not a finished BR as actual data needs to be substituted for object attributes and glide records need to populate LineItem data with PO lines. It does, however, provide the structure needed to format the REST API post structure in my original message.
======
(function executeRule(current, previous /*null when async*/) {
try {
var _master = {
"MT_WEBOrderCreateRequest": {
"OrderCreateRequest": {
}
}
};
var _OrderCreateRequest = { "SalesOrderCreate": {} };
_OrderCreateRequest.ClientID = "clientID-001";
_OrderCreateRequest.ClientTransactionID = current.number.toString();
//Move OrderCreateRequest into MT_WEB obj
_master.MT_WEBOrderCreateRequest.OrderCreateRequest = _OrderCreateRequest;
var _order = {
"OrderHeader": {},
"LineItems": []
};
var _header = { "BusinessPartnerHeader": [], "SmartTrackerHeader": [] };
_header.CustomerOrderNumber = current.number.toString();
_header.CustomerOrderDate = current.number.toString();
_header.ReleaseNumber = current.number.toString();
_header.SoldToNumber = '10314198';
_header.ContactPerson = current.number.toString();
_header.CurrencyCode = 'USD';
_header.RequestDeliveryDate = current.number.toString();
_header.ShippingMethod = current.number.toString();
_header.ShippingCarrier = current.number.toString();
_header.CarrierAccount = current.number.toString();
_header.ASNEmail = current.number.toString();
_header.SpecialProcIndicator = current.number.toString();
_header.TextSpecialProcIndicator = current.number.toString();
_header.TextSpecialInstructionsHold = current.number.toString();
var _businesspartner = {};
_businesspartner.PartnerFunction = "func1";
_businesspartner.PartnerNumber = "no1";
_businesspartner.Name = "name1";
_businesspartner.Name2 = "name2";
_businesspartner.Attention = "attn1";
_businesspartner.Street = "street1";
_businesspartner.Street2 = "street2";
_businesspartner.City = "salida";
_businesspartner.PostalCode = "81201";
_businesspartner.Region = "region1";
_businesspartner.CountryCode = "code1";
var _smartheader = {};
_smartheader.Key = "smart1";
_smartheader.Value = "value1";
var _item = { "SmartTrackerItem": []};
_item.MaterialNumber= current.number.toString();
_item.CustMaterialNumber = current.number.toString();
_item.LineNumber = current.number.toString();
_item.HighLevelItem = current.number.toString();
_item.Quantity = '5';
_item.SalesUoM = current.number.toString();
_item.CustExpectedPrice = "99.99";
_item.RegistrationContactName = current.number.toString();
_item.RegistrationContactPhoneNumber = current.number.toString();
_item.RegistrationContactEmail = current.number.toString();
_item.RequestDeliveryDate = current.number.toString();
_item.AgreementNumber = current.number.toString();
_item.Contract = current.number.toString();
var _smartitem = {};
_smartitem.Key = "smartkey1";
_smartitem.Value = "smartvalue1";
//push smart heading data into header
_header.SmartTrackerHeader.push(_smartheader);
//push business partner data into header
_header.BusinessPartnerHeader.push(_businesspartner);
//Move header into order
_order.OrderHeader = _header;
//push multiple items into the line items array
_order.LineItems.push(_item);
//push 2nd order line into the line items array
_order.LineItems.push(_item);
//push multiple orders into the SalesOrderCreate array.
//_master.MT_WEBOrderCreateRequest.OrderCreateRequest.SalesOrderCreate.push(_order);
_OrderCreateRequest.SalesOrderCreate = _order;
var body = JSON.stringify(_master);
var r = new sn_ws.RESTMessageV2('Insight Sales Order Processing', 'post');
//override authentication profile
var authentication_type = 'oauth2';
var profile_name = 'Insight OAuth default_profile';
r.setAuthentication(authentication_type, profile_name);
//
r.setRequestBody(body);
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
}
catch(ex) {
var message = ex.getMessage();
}
})(current, previous);