ServiceNow Phone Image

Attaching JSON in ServiceNow using REST Messages

During your development in the ServiceNow platform, you may encounter the need for data to be uploaded to the server, and manipulated, but not stored as records in a user created table, or any of the pre-built tables. This post is specifically designed for users wanting to add JSON or any attachment file to a record in a table through a workflow.

Step 1: Build the REST Message

Search in the Navigator bar and find “Outbound -> REST Message” to create a new Outbound REST Message.
*Name: Generate Attachment File
Description: Include an attachment on a given record in a given table

Make sure you set up your “Authentication Type”.

Now right click the menu at the top and select “Save” from the dropdown, this will refresh your page and generate the HTTP Methods table.

Step 2: Generate a new GlideRecord in a new Run Script

Within the workflow, we are going to want to create a new record on a table for you to attach the JSON file to. In this case, lets set up a simple GlideRecord on the “sc_tasks” table. Go to “core” in the top right of the workflow editor screen, and type in “script” in the search box. Select Run Script from the options and drag and drop it into your workflow. Now inside, give it a proper name such as “Create a new Task”, and make sure you assign a stage to it.

Now for the script:

//Obtain Request and RITM numbers in the workflow
var request_num = current.request;
var ritm_num = current.sys_id;

var gr = new GlideRecord("sc_task");
gr.short_description = "Attachment Task";
gr.request_item = ritm_num;

//Set a workflow variable to the sys_id of the task item
workflow.scratchpad.new_task = gr.sys_id;


Step 3: Run the ‘POST’ RESTMessageV2

Now that we’ve set up a block that generates a new Task item in ServiceNow, let’s work on actually attaching some JSON data to it. Go ahead and create another Run Script in your workflow, following the one you have made. Let’s title this one “Add attachment to task record” and get ready to write some code :)!

var request = new sn_ws.RESTMessageV2();
var table_name = "sc_task";
var table_sys_id = workflow.scratchpad.new_task;
var file_name = "Sample.json";
var instance_name = gs.getProperty("instance_name");
var jsonblock = {"sample_data": "I am json!"};

request.setEndpoint(instance_name + "");
request.setStringParameterNoEscape("table_name", table_name);
request.setStringParameterNoEscape("table_sys_id", table_sys_id);
request.setStringParameterNoEscape("file_name", file_name);

var user = "username";
var password = "password";

request.setBasicAuth(user, password);
request.setRequestHeader("Accept", "application/json");

var response = request.execute();

workflow.scratchpad.successfully_attached = response.getStatusCode();

Now run through submit your catalog item that you attached this workflow to and watch your attachment file be populated!

Best of luck!

Leave a Reply

Your email address will not be published. Required fields are marked *