Fetch RITM Variable and attachment added to the same using REST API

Deepika Mishra
Mega Guru

Hi All,

I have an requirement where for a particular RITM I need to fetch the variable values along with the attachment added to that RITM.
I am able to fetch RITM variable by following below way:

  • Namespace: now
  • API Name: Table API
  • API Version: Latest
  • Retrieve records from a table (GET)
  • tableName: sc_req_item
  • sysparm_query: number=RITM0000001^active=true
  • sysparm_display_value: true
  • sysparm_fields: number,assignment_group,variables.variableA,variables.variableB

 

On following above way I am getting the value, but I also want to fetch the attachment in the RITM (not the name the file).
I am very new to integration, so I am looking if someone can guide me step by step process on the same.

1 ACCEPTED SOLUTION

Deepika Mishra
Mega Guru

As it for the same system ,thus, I created a Scripted REST API and it worked:

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

    // Get RITM number from the request parameters
    var ritmNumber = request.queryParams.number;

    // Fetch RITM variables
    var ritmRecord = new GlideRecord('sc_req_item');
    ritmRecord.addQuery('number', ritmNumber);
    ritmRecord.query();

    if (ritmRecord.next()) {
        // Fetch variables
        var variables = {
            variableA: ritmRecord.variables.variableA,
            variableB: ritmRecord.variables.variableB
            // Add other variables as needed
        };

        // Fetch attachments
        var attachments = [];
        var attachmentRecord = new GlideRecord('sys_attachment');
        attachmentRecord.addQuery('table_sys_id', ritmRecord.sys_id.toString());
        attachmentRecord.query();

        while (attachmentRecord.next()) {
            //var attachmentLink = gs.getProperty('glide.servlet.uri') + attachmentRecord.sys_id;
			var attachmentLink = gs.getProperty('glide.servlet.uri') + 'sys_attachment.do?sys_id=' + attachmentRecord.sys_id;
            attachments.push({
                file_name: attachmentRecord.file_name.toString(),
                attachment_link: attachmentLink
                // Add other attachment details as needed
            });
        }

        // Prepare response
        var result = {
            ritm_number: ritmRecord.number,
            assignment_group: ritmRecord.assignment_group.getDisplayValue(),
            variables: variables,
            attachments: attachments
        };

        // Send the response
        response.setBody(result);
    } else {
        // RITM not found
        response.setError(404, 'RITM not found');
    }

})(request, response);

View solution in original post

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@Deepika Mishra 

who will be consuming the endpoint? an external system?

to get attachment data you need to use Attachment API as attachments are stored in sys_attachment table. Use the RITM sysId to query in the Attachment API

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Deepika Mishra
Mega Guru

As it for the same system ,thus, I created a Scripted REST API and it worked:

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

    // Get RITM number from the request parameters
    var ritmNumber = request.queryParams.number;

    // Fetch RITM variables
    var ritmRecord = new GlideRecord('sc_req_item');
    ritmRecord.addQuery('number', ritmNumber);
    ritmRecord.query();

    if (ritmRecord.next()) {
        // Fetch variables
        var variables = {
            variableA: ritmRecord.variables.variableA,
            variableB: ritmRecord.variables.variableB
            // Add other variables as needed
        };

        // Fetch attachments
        var attachments = [];
        var attachmentRecord = new GlideRecord('sys_attachment');
        attachmentRecord.addQuery('table_sys_id', ritmRecord.sys_id.toString());
        attachmentRecord.query();

        while (attachmentRecord.next()) {
            //var attachmentLink = gs.getProperty('glide.servlet.uri') + attachmentRecord.sys_id;
			var attachmentLink = gs.getProperty('glide.servlet.uri') + 'sys_attachment.do?sys_id=' + attachmentRecord.sys_id;
            attachments.push({
                file_name: attachmentRecord.file_name.toString(),
                attachment_link: attachmentLink
                // Add other attachment details as needed
            });
        }

        // Prepare response
        var result = {
            ritm_number: ritmRecord.number,
            assignment_group: ritmRecord.assignment_group.getDisplayValue(),
            variables: variables,
            attachments: attachments
        };

        // Send the response
        response.setBody(result);
    } else {
        // RITM not found
        response.setError(404, 'RITM not found');
    }

})(request, response);