JSON object in for loop
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2017 07:14 AM
Hi All,
I have a json object inside which der is an array called as items which is used to store the RITM of a request.
But suppose i have 4 RITM for a request how can i dynamically create 4 elements inside the array with these field values.
{
"Items": [{
"MaterialNumber": "3000016264859",
"CustMaterialNumber": "RITM00098",
"LineNumber": "1",
"HighLevelItem": "",
"Quantity": "2",
"SmartTrackerItem": [{
"Key": "",
"Value": ""
}]
}]
}
Thanks in advance.
Abinash Dash
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-27-2017 02:26 AM
Thanks adam...it really helped but the thing is am not able to map the values means the am unable to map my ritm number with a mapped field.
here is my sample code:
correct me if am wrong anywhere
payload.Items=[];
var ritmRec = new GlideRecord('sc_req_item');
ritmRec.addQuery('request','0166b93d4f4a030021aa8fe18110c7ff'); // i have 2 ritm inside this request but my ritm numbers are not mapping to the field which i want to pass
ritmRec.query();
while(ritmRec.next()) {
gs.print('numbers'+ritmRec.number)
itemObj = {};
itemObj.CustMaterialNumber=ritmRec.number;
itemObj.test="hello";
payload.Items.push(itemObj);
}
// var json = JSON.stringify(jsonObj);
//var parser = JSON.parse(json);
//gs.print(parser);
var json = JSON.stringify(payload);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-27-2017 07:30 AM
Are the print statements showing the correct output?
One thing you could try is to add .toString() at the end of "itemObj.CustMaterialNumber=ritmRec.number". This is usually the first thing I'll do when I see this strange behavior. I believe it has something to do with how javascript handles pointers and objects but simply adding .toString() fixes a strangely high number of issues when dealing with GlideRecord data.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-30-2017 12:43 AM
Hello Adam,
Yes i parsed them into string format and giving me the desired outputs.. thanxx a lot for ur help..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-06-2017 01:51 AM
Hi Adam, awessel
How can i Insert another array inside that array so as to get this output:
"Items": [{
"MaterialNumber": "3000016264859",
"CustMaterialNumber": "RITM00098",
"LineNumber": "1",
"HighLevelItem": "",
"Quantity": "2",
"SmartTrackerItem": [{
"Key": "",
"Value": ""
}]
}]
}
I have this smart tracker array and it should populate the values for each ritm..
