- 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 10:00 AM
Hi John,
var object_data = "";
var sValues = "";
var sJoin = "";
var fields = current.getFields();
for (var i = 0; i < field.size(); i++){
var field = fields.get(i);
var name = field.getName(i);
var value = field.getDisplayValue();
sValues = sValues + sJoin + "'" + name + "':'" + GlideString.escapeHTML(valye) + "'";
sJoin = ",";
}
object_data = "{" + sValues + "}";
var obj = new JSON().encode(object_data);
var r = new sn_ws.RESTMessageV2('TEST_ACCESS_DEV23693', 'post");
r.setStringParameter("InstanceName",gs.getProperty('instance_name'));
r.setXMLParameterNoEscape("object_data",obj);
r.setHttpTimeout(10000);
var response=r.execute();
var responseBody= response.getBody();
status = response.getStatusCode();
}
catch(e)
{
gs.log("exception" + e)
}
This should be the sample one. Please see if it helps.
Regards
Param
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-21-2017 11:18 AM
Thanks Paramahanns for your reply. Can you dial in your advice to show how to construct the JSON structure so that the var mainObj contains each object and array within the MWOrderCreateRequest object?
- 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 03:16 PM
Jim, let me look this over tonight and play with it. I do appreciate your response...it looks like it is going to work.