- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-04-2023 12:33 PM
Hi Below is my requirement
above is the attachment data i have .. in this i need to get the details of type( how many records count in user & group) for this below is the script i written but it's not working
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-05-2023 09:45 AM
@Rama26 Please try the following script and it should get you the right count.
var attach = new GlideRecord('sys_attachment');
attach.addQuery('sys_id', '6dc8f4f9971631101130b696f053afa6');
attach.setWorkflow(false);
attach.query();
var userCount = 0;
if (attach.next()) {
var filename = attach.getValue('file_name');
var id = attach.sys_id;
var arr = [];
var parser = new sn_impex.GlideExcelParser();
var attachment = new GlideSysAttachment();
var attachmentStream = attachment.getContentStream(id);
parser.parse(attachmentStream);
var headers = parser.getColumnHeaders();
var header1 = headers[1];//Header 0 contain Details, Header 1 contains Type
while (parser.next()) {
var row = parser.getRow();
var value1 = row[header1];
if (value1 && value1.toLowerCase() === 'user') {
userCount++;
}
}
gs.info('User Count: ' + userCount);
}
I found two issues with the script you shared with your question
1. As rightly pointed by others it was comparing lowercase value with 'User' which was failing
2. On line number 15 var header1 = headers[0]; you are fetching header at 0th index which is Details, instead you should assign var header1 = headers[1] which is type with which the comparison should be made.
Please do not forget to mark this answer helpful and correct if it manages to answer your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-04-2023 01:47 PM
Add more log statements inside the if and while blocks to see if the script is making it inside these, or if it's failing at the final if statement, since you're converting the value to lower then stating that it must be exactly 'User', which is not lower case, so keep it simple and try:
if (value1.toLowerCase() == 'user') {
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-04-2023 10:38 PM
Hello @Rama26
The cursor of your code is not entering into the if block, therefore count is coming as 0. The reason is you are comparing lowercase with 'User', Where U is capital.
Pls modify your code :-
var attach = new GlideRecord('sys_attachment');
attach.addQuery('sys_id', '6dc8f4f9971631101130b696f053afa6');
attach.setWorkflow(false);
attach.query();
var userCount = '';
if (attach.next()) {
var filename = attach.getValue('file_name');
var id = attach.sys_id;
var arr = [];
var parser = new sn_impex.GlideExcelParser();
var attachment = new GlideSysAttachment();
var attachmentStream = attachment.getContentStream(id);
parser.parse(attachmentStream);
var headers = parser.getColumnHeaders();
var header1 = headers[0];
while (parser.next()) {
var row = parser.getRow();
var value1 = row[header1];
if (value1.toLowerCase() === 'user') {
userCount++;
}
}
gs.info('User Count: ' + userCount);
}
Plz mark my solution as Accept, If you find it helpful.
Regards,
Samaksh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-05-2023 09:21 AM
HI I changed my script but still it's not working

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-05-2023 09:45 AM
@Rama26 Please try the following script and it should get you the right count.
var attach = new GlideRecord('sys_attachment');
attach.addQuery('sys_id', '6dc8f4f9971631101130b696f053afa6');
attach.setWorkflow(false);
attach.query();
var userCount = 0;
if (attach.next()) {
var filename = attach.getValue('file_name');
var id = attach.sys_id;
var arr = [];
var parser = new sn_impex.GlideExcelParser();
var attachment = new GlideSysAttachment();
var attachmentStream = attachment.getContentStream(id);
parser.parse(attachmentStream);
var headers = parser.getColumnHeaders();
var header1 = headers[1];//Header 0 contain Details, Header 1 contains Type
while (parser.next()) {
var row = parser.getRow();
var value1 = row[header1];
if (value1 && value1.toLowerCase() === 'user') {
userCount++;
}
}
gs.info('User Count: ' + userCount);
}
I found two issues with the script you shared with your question
1. As rightly pointed by others it was comparing lowercase value with 'User' which was failing
2. On line number 15 var header1 = headers[0]; you are fetching header at 0th index which is Details, instead you should assign var header1 = headers[1] which is type with which the comparison should be made.
Please do not forget to mark this answer helpful and correct if it manages to answer your question.