Attachement name and format is undefined when it is attaching to the case

Uday Kumar1
Tera Contributor

Hello experts,

 

I used the below script to generate the CSV file using a BR but the attachment was undefined when it is attaching to the case.

 

@asifnoor Could you please check the code once and help us to proceed further.

 

Please find the below script that I used.

 

(function executeRule(current, previous /*null when async*/) {
var Headers = ["Number"];
var fileName = 'ADP11.csv';
var csvData = ''; //The variable csvData will contain a string which is used to build the CSV file contents
for (var i = 0; i < Headers.length; i++) { //Build the Headers
    csvData = csvData + '"' + Headers[i] + '"' + ',';
}
csvData = csvData+"\r\n";

var gr = new GlideRecord("xxxxxxxx");
gr.addQuery("u_parent_hr_case_no", current.number);
//gr.addEncodedQuery('sys_idSTARTSWITH31df24e34754d254d3a0a2e4116d439c');
gr.query();
//u_parent_hr_case_number=HRC2864702
while(gr.next()) {
    csvData = csvData + '"' + gr.u_gg_int_record_type + '"' + '|' + gr.u_gg_int_record_type_0021 + '|' + gr.u_gg_int_record_type_015 + '|' + gr.u_gg_int_record_type_2010 + '|' + gr.u_gg_int_record_type_trail + '|' + gr.u_gg_user_id + '|' + gr.u_previous_personnel_number + '|' + gr.u_contact_name + '|' + gr.u_country_iso_code + '|' + gr.u_gg_int_file_name + '|' + gr.u_infotype_operation + '|' + gr.u_gg_interface_run_date + '|' + gr.u_infotype_0221 + '|' + gr.u_gg_interface_run_time + '|' + gr.u_subtype + '|' + gr.u_run_type + '|' + gr.u_start_date + '|' + gr.u_version_number + '|' + gr.u_end_date + '|' + gr.u_currency_key + '|' + gr.u_wage_type + '|' + gr.u_wage_type_amount_for_payments + '|' + gr.u_record_count;
    csvData = csvData+"\r\n";
}

//attach the file to a record.
var grRec = new GlideRecord("xxxxxxxxxxx");
grRec.addQuery("u_parent_hr_case_no", current.number);
grRec.query();
if(grRec.next()){

    if(grRec.u_gg_int_record_type == 'HEADER') {
    var grAttachment = new GlideSysAttachment();
    grAttachment.write(grRec, fileName, 'application/csv',csvData);
}
    // Add your code here

}})(current, previous);
 
 

https://www.servicenow.com/community/developer-articles/generate-csv-file-through-script/ta-p/231977...

2 REPLIES 2

New Developer_S
Giga Sage

@Uday Kumar1 

try Updating  the CSV construction to use join(',') to properly format the data.

my code :

 

 

 

(function executeRule(current, previous /*null when async*/) {
// Define the CSV Headers and initialize the CSV data variable
var Headers = ["Number", "Record Type", "Field 0021", "Field 015", "Field 2010", "Trail", "User ID", "Personnel Number", "Contact Name", "Country ISO", "File Name", "Infotype Operation", "Interface Run Date", "Infotype 0221", "Interface Run Time", "Subtype", "Run Type", "Start Date", "Version Number", "End Date", "Currency Key", "Wage Type", "Wage Type Amount", "Record Count"];
var fileName = 'ADP11.csv';
var csvData = Headers.join(',') + '\r\n'; // Join headers with commas for CSV format

// Query the relevant records to include in the CSV
var gr = new GlideRecord("xxxxxxxx"); // Replace xxxxxxxx with the correct table name
gr.addQuery("u_parent_hr_case_no", current.number);
gr.query();

// Loop through the results and build CSV rows
while(gr.next()) {
var row = [
gr.u_gg_int_record_type,
gr.u_gg_int_record_type_0021,
gr.u_gg_int_record_type_015,
gr.u_gg_int_record_type_2010,
gr.u_gg_int_record_type_trail,
gr.u_gg_user_id,
gr.u_previous_personnel_number,
gr.u_contact_name,
gr.u_country_iso_code,
gr.u_gg_int_file_name,
gr.u_infotype_operation,
gr.u_gg_interface_run_date,
gr.u_infotype_0221,
gr.u_gg_interface_run_time,
gr.u_subtype,
gr.u_run_type,
gr.u_start_date,
gr.u_version_number,
gr.u_end_date,
gr.u_currency_key,
gr.u_wage_type,
gr.u_wage_type_amount_for_payments,
gr.u_record_count
].join(','); // Join fields with commas for CSV format
csvData += row + '\r\n';
}

// Attach the file to the record
var grRec = new GlideRecord("xxxxxxxxxxx"); // Replace xxxxxxxxxx with the correct table name for the case
grRec.addQuery("u_parent_hr_case_no", current.number);
grRec.query();

if(grRec.next()){
var grAttachment = new GlideSysAttachment();
grAttachment.write(grRec, fileName, 'application/csv', csvData); // Attach CSV data as a file
gs.info("CSV file attached successfully: " + fileName); // Log success for debugging
} else {
gs.error("Failed to find record to attach CSV.");
}

})(current, previous);

 

Accept and hit Helpful if it helps.

Best Regards

Uday Kumar1
Tera Contributor

Hi @New Developer_S ,

 

Can we add multiple field names in each row of the attachment for each record.

 

Thanks,

Uday Kumar