Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

JSON object in for loop

Abinash
Kilo Contributor

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

Developer Community

8 REPLIES 8

Abinash
Kilo Contributor

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);


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.


Abinash
Kilo Contributor

Hello Adam,


Yes i parsed them into string format and giving me the desired outputs.. thanxx a lot for ur help..


Abinash
Kilo Contributor

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..