How to remove empty objects from array of objects?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-26-2020 03:38 AM
Hi,
In an integration , I have to pass the sys_id and fetch the attachments of that record.
-some records have multiple attachments and some have none
-Passing the sys_id in for loop.
Issue : Getting empty arrays along with arrays that has values
below is my script include
getAttachment : function(arrayString){
var r = new sn_ws.RESTMessageV2('Knowledge Integration for Bulk upload', 'Get Article');
r.setStringParameter('Accept', 'application/json');
r.setEndpoint('https://bayersidev.service-now.com/api/sn_km_api/knowledge/articles/'+arrayString);
var response = r.execute();
var responseBody = response.getBody();
var parser = new JSONParser();
var parsed = parser.parse(responseBody);
var recordSysId=[];
if(parsed.result.attachments.length !=undefined)
{
var contentArr = [];
var num = new GlideRecord(parsed.result.template_table);
num.addQuery("u_bayer_fmo_number",parsed.result.number);
num.query();
if(num.next()){
recordSysId =num.sys_id;
}
var attachmentLength = parsed.result.attachments.length;
for(var n =0;n<attachmentLength;n++)
{
var attachment ={};
attachment.attachmentID=parsed.result.attachments[n].sys_id;
attachment.sysid=parsed.result.sys_id;
attachment.file_name = parsed.result.attachments[n].file_name;
attachment.size_bytes = parsed.result.attachments[n].size_bytes;
attachment.table=parsed.result.template_table;
attachment.recordSysId = recordSysId;
contentArr.push(attachment);
}
gs.log("attachment json :"+contentArr);
return JSON.stringify(contentArr);
}
},
Below is my scheduled job
var arrayString=[],contentArr=[],attachmentMap=[],embeddedID=[],embedContentMap=[];
var kbUpload = new KnowledgeBulkUpload;
arrayString = kbUpload.searchArticle();
gs.log("ArrayString :"+arrayString.length);//
for(var a=0;a<arrayString.length;a++){
kbUpload.getArticle(arrayString[a]);}
for(var b=0;b<arrayString.length;b++){
contentArr = kbUpload.getAttachment(arrayString[b]);
}
gs.log("Attachments :"+contentArr.length);
for(var i=0;i<contentArr.length;i++){
attachmentMap = kbUpload.attachmentUpdate(contentArr[i]);}
below are the logs
Attachments :[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][{"attachmentID":"128c35941b9d14541c6442eacd4bcbf2","sysid":"828cf9541b9d14541c6442eacd4bcbc4","file_name":"JIRA Workflow Demand Management (1).pptx","size_bytes":"576420","table":"u_kb_template_end_user"},{"attachmentID":"2e8cb9941b9d14541c6442eacd4bcb4c","sysid":"828cf9541b9d14541c6442eacd4bcbc4","file_name":"Workflow Status Matrix.xlsm","size_bytes":"101326","table":"u_kb_template_end_user"},{"attachmentID":"5e8c35941b9d14541c6442eacd4bcbf0","sysid":"828cf9541b9d14541c6442eacd4bcbc4","file_name":"JIRA with myCloud.docx","size_bytes":"736265","table":"u_kb_template_end_user"}],[],[],[]
attachment json :
How do I eliminate the empty ones and pass only the ones with value to my script include? I need to pass each json object to another function and it should run only three times as obj.length.
Please help.
Thanks & Regards,
Chinmayee Mishra
- Labels:
-
Integrations
-
Script Debugger

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-26-2020 07:08 AM
Hi,
Can you share the output log of ths line which is there in your SI.
gs.log("attachment json :"+contentArr);
Also, your scheduled job log that you posted above is not right. AS you are printing the lenght, but showing na array.
gs.log("Attachments :"+contentArr.length);
Canhyou cross verify and share the details again.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-26-2020 07:16 AM
I have pasted an image for attachment json.The scheduled job has no issues . Servicenow will never print values if I give object.length. I have posted it incorrectly. I kept trying for values and length.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-26-2020 07:50 AM
Hello Chinmayee,
Made few changes in your SI and schedueld job. Can you run this and share the logs output.
Go to logs and search by Test: and share the screenshot of the logs.
Script Include
getAttachment : function(arrayString){
var r = new sn_ws.RESTMessageV2('Knowledge Integration for Bulk upload', 'Get Article');
r.setStringParameter('Accept', 'application/json');
r.setEndpoint('https://bayersidev.service-now.com/api/sn_km_api/knowledge/articles/'+arrayString);
var response = r.execute();
var responseBody = response.getBody();
var parser = new JSONParser();
var parsed = parser.parse(responseBody);
var recordSysId="";
gs.info("Test: parsed results "+parsed.result.attachments.length);
if(parsed.result.attachments.length !=undefined)
{
var contentArr = [];
var num = new GlideRecord(parsed.result.template_table);
num.addQuery("u_bayer_fmo_number",parsed.result.number);
num.query();
if(num.next()){
recordSysId =num.sys_id;
}
gs.info("Test: Record sys id is "+recordSysId);
var attachmentLength = parsed.result.attachments.length;
for(var n =0;n<attachmentLength;n++)
{
var attachment ={};
attachment.attachmentID=parsed.result.attachments[n].sys_id;
attachment.sysid=parsed.result.sys_id;
attachment.file_name = parsed.result.attachments[n].file_name;
attachment.size_bytes = parsed.result.attachments[n].size_bytes;
attachment.table=parsed.result.template_table;
attachment.recordSysId = recordSysId;
contentArr.push(attachment);
}
gs.info("Test: attachment json :"+contentArr);
return JSON.stringify(contentArr);
}
},
Scheduled job
var arrayString=[],contentArr=[],attachmentMap=[],embeddedID=[],embedContentMap=[];
var kbUpload = new KnowledgeBulkUpload;
arrayString = kbUpload.searchArticle();
gs.log("Test: ArrayString :"+arrayString.length);//
for(var a=0;a<arrayString.length;a++){
kbUpload.getArticle(arrayString[a]);
}
for(var b=0;b<arrayString.length;b++){
contentArr.push(kbUpload.getAttachment(arrayString[b]));
}
gs.log("Test: Attachments :"+contentArr);
for(var i=0;i<contentArr.length;i++){
attachmentMap = kbUpload.attachmentUpdate(contentArr[i]);
}