- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-27-2020 12:33 AM
I have the following array of objects
[{"Device":"Apple","Owner":"Joe Employee"},{"Device":"Microsoft","Owner":"Jack Businessman"},{"Device":"Huawei","Owner":"Patel Employee"}]
and need to insert insert 3 records to my table into fields, Device and Owner, how can I achieve this?
DEVICE | OWNER | |
RECORD1 | Apple | Joe Employee |
RECORD2 | Microsoft | Jack Businessman |
RECORD3 | Huawei | Patel Employee |
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-27-2020 12:51 AM
Something like below. Replace "<table name>" with the name of table to insert into.
var jsonString = '[{"Device":"Apple","Owner":"Joe Employee"},{"Device":"Microsoft","Owner":"Jack Businessman"},{"Device":"Huawei","Owner":"Patel Employee"}]';
var jsonObj = JSON.parse(jsonString);
for (var i=0;i<jsonObj.length;i++) {
var device = jsonObj[i]['Device'];
var owner = jsonObj[i]['Owner'];
var gr = new GlideRecord("<table name>");
gr.initialize();
gr.device = device;
gr.owner = owner;
gr.insert();
}
EDIT:
Refer to following GlideRecord document on how to insert values.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-27-2020 12:44 AM
Check this blog it will be helpful,
https://community.servicenow.com/community?id=community_blog&sys_id=aedbb9e8dbafbb805ed4a851ca9619ba
Post parsing you can glide to insert the record in table
https://developer.servicenow.com/dev.do#!/reference/api/orlando/client/c_GlideRecordClientSideAPI
insert(Function responseFunction)
Mark my ANSWER as CORRECT and HELPFUL if it helps

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-27-2020 12:51 AM
Something like below. Replace "<table name>" with the name of table to insert into.
var jsonString = '[{"Device":"Apple","Owner":"Joe Employee"},{"Device":"Microsoft","Owner":"Jack Businessman"},{"Device":"Huawei","Owner":"Patel Employee"}]';
var jsonObj = JSON.parse(jsonString);
for (var i=0;i<jsonObj.length;i++) {
var device = jsonObj[i]['Device'];
var owner = jsonObj[i]['Owner'];
var gr = new GlideRecord("<table name>");
gr.initialize();
gr.device = device;
gr.owner = owner;
gr.insert();
}
EDIT:
Refer to following GlideRecord document on how to insert values.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-27-2020 01:00 AM
Hi,
sample script below
var json = '[{"Device":"Apple","Owner":"Joe Employee"},{"Device":"Microsoft","Owner":"Jack Businessman"},{"Device":"Huawei","Owner":"Patel Employee"}]';
var jsonObj = JSON.parse(json);
for (var i=0;i<jsonObj.length;i++) {
var device = jsonObj[i].Device;
var owner = jsonObj[i].Owner;
var gr = new GlideRecord("table"); // give valid table name here
gr.initialize();
gr.setValue('field1',device); // give valid field name here
gr.setValue('field2',owner); // give valid field name here
gr.insert();
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader