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.

Create an excel with two columns

Miriam Zaragoz1
Kilo Explorer

Hello,

I have created a Record producer to generate an excell. Currently the excell is created without problem but I have all the data in the same column and I would like it to be created in two separate columns.

The code used to create the excell is:

 

var attachment = new Attachment();
var fileName = 'Accounts for ' + cs_user.user.name + '.csv';
var content_type = 'text/csv';
var fileBytes = 'Name,Number\n';

                    ...

    var trans_name = gr.name.split(' ');
    if (trans_name[1] == '' || trans_name[1] === undefined) {
        var trans_name2 = trans_name[0];
        if (array_accounts.indexOf(trans_name2) < 0) {
            array_accounts.push(trans_name2);
            array_count.push(1);
        } else {
            array_count[array_accounts.indexOf(trans_name2)] += 1;
        }
    } else {
        var trans_name2 = trans_name[0] + ' ' + trans_name[1];
        if (array_accounts.indexOf(trans_name2) < 0) {
            array_accounts.push(trans_name2);
            array_count.push(1);
        } else {
            array_count[array_accounts.indexOf(trans_name2)] += 1;
        }
    }

}

for (var i = 0; i < array_accounts.length; i++) {
    fileBytes += array_accounts[i] + ',' + array_count[i] + '\n';
}
//Attachment attached
var attach_url = attachment.write('sys_user_grmember', cs_user.sys_id, fileName, content_type, fileBytes);

 

I think that the key is fileBytes but I dont know how to do it. Anyone can help me?

 

Thanks.

1 REPLY 1

Martin iTSM
Tera Guru

Hey Miriam,

how do you open the file?
You cannot just double click a .csv and load it into excel. You can - but it won`t format it correctly that way.

You have to open an (empty) excel sheet and go to > Data > From text/csv 
From there one you can do different settings before the import/load i.e. which character is used to separate the individual chunks of data etc.

The .csv should look roughly like this:

Name, Number
Martin, 1
Paul, 2
Bert, 3

Then the above described procedure should work just fine.

Hope this helps!

Cheers!

Martin