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.

Trouble with Custom Scope UI Action Script for CSV File Creation

senon
Tera Sage
Tera Sage

Hi Experts.

I want to use a UI action to create a CSV file containing all the field labels and field values recorded in the current record and attach it to the record.

I understand that this UI action is created with a custom scope and therefore has scripting limitations.

I created the following script, but it is not working.

 

// This script is designed to be used as a UI action in ServiceNow.
// It retrieves the field labels and values of the current record and creates a CSV file for the record.
// The application scope is custom, so it is not global.

var table = "<table_name>";
var recordId = current.sys_id;
var output = "";

var elementLabelArr = current.getElements(); 
var elementValueArr = current.getElements();

var fldsLabel = [];
var fldsValue = [];

// Move the field names and values into JavaScript Arrays...
for (var i = 0; i < elementLabelArr.length; i++) {
    // Push the field labels into the array
    fldsLabel.push(elementLabelArr[i].getLabel());
    // Push the field values into the array
    fldsValue.push(elementValueArr[i].getDisplayValue());
}

// Prepare the CSV output
output += fldsLabel.join(",") + "\n";
output += fldsValue.join(",") + "\n";

// Call the function to write the CSV as an attachment for the current record
writeAttachmentFile(output);

// Function to write the CSV file as an attachment
function writeAttachmentFile(data) {
    // Create a new instance of GlideSysAttachment
    var attachment = new GlideSysAttachment();
    // Write the CSV data as an attachment for the current record
    var attachmentRec = attachment.write(table, recordId, "export.csv", "text/csv", data);
}

 

Am I on the right track?
Please help me if you have a problem with this script and can help me figure it out.

 

Best Regards.

Senon

3 REPLIES 3

Maik Skoddow
Tera Patron
Tera Patron

Hi @senon 

 

basically you only have to copy and modify the code given in the examples of the API documentation.

 

The following script is doing the job:

 

var fldsLabel = [];
var fldsValue = [];
var fields    = current.getFields();
var output    = '';

for (var i = 0; i < fields.size(); i++) {
  var glideElement = fields.get(i);

  fldsLabel.push(glideElement.getLabel());
  fldsValue.push(glideElement.getDisplayValue()); 
}

// Prepare the CSV output
output += fldsLabel.join(",") + "\n";
output += fldsValue.join(",") + "\n";

// Create a new instance of GlideSysAttachment
var attachment = new GlideSysAttachment();
// Write the CSV data as an attachment for the current record
var attachmentRec = attachment.write(current, "export.csv", "text/csv", output);

 

 Maik

@Maik Skoddow 

Hi, thanks for your reply.

This script is available in global scope, but I need to realize it in custom scope.

var fields = current.getFields();

The above script is not available in custom scope.
Is there any way to realize it in custom scope?

Hi @senon 

 

again, a look into the API document is helpful. There is a helper script include which does the job in global as well as in custom scopes. The modified script now looks as follows:

 

var fldsLabel = [];
var fldsValue = [];
var fields    = new global.GlideRecordUtil().getFields(current);
var output    = '';

for (var i = 0; i < fields.length; i++) {
  var glideElement = current.getElement(fields[i]);

  fldsLabel.push(glideElement.getLabel());
  fldsValue.push(glideElement.getDisplayValue()); 
}

// Prepare the CSV output
output += fldsLabel.join(",") + "\n";
output += fldsValue.join(",") + "\n";

// Create a new instance of GlideSysAttachment
var attachment = new GlideSysAttachment();
// Write the CSV data as an attachment for the current record
var attachmentRec = attachment.write(current, "export.csv", "text/csv", output);

 

 Maik