i am unable to get the count of attachment

Rama26
Tera Contributor

Hi Below is my requirement

Rama26_0-1699126240546.png

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

 

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 && value1.toLowerCase() === 'User') {
        userCount++;
    }
}
gs.info('User Count: ' + userCount);
} in User Count  i am receiving count is 0 instead of 2.
1 ACCEPTED SOLUTION

Sandeep Rajput
Tera Patron
Tera Patron

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

View solution in original post

4 REPLIES 4

Brad Bowman
Kilo Patron
Kilo Patron

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') {

 

Samaksh Wani
Giga Sage
Giga Sage

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

HI I changed my script but still it's not working

Sandeep Rajput
Tera Patron
Tera Patron

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