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.

Get attachments from Third party to ServiceNow using Rest API

Ashok32
Tera Contributor

Hi All,

 

I have a requirement to get all the attachments which are attached by the Third party in their tool and update the same attachments in the incident record in ServiceNow.

 

For that I use Get method and I'm passing the below details are the Request headers.

 

Content-Type =<which specifies the content type of a file>

X-MessageType = file

X-FileName =<specifies the file name that they are attched from their end>

X-MessageId =<ID which they passed as unique>

X-EntityType= incident

X-Action = update

X-Integration =<they provided the details>

 

So can someone help me the script to achieve this requirement.

 

 

Thanks in advance.

3 REPLIES 3

Abbas_5
Tera Sage
Tera Sage

Hello @Ashok32,
Please refer to the below link:
https://www.servicenow.com/community/developer-forum/rest-api-from-third-party-and-get-attachment/m-....

 

Mark my correct and helpful, if it is helpful and please hit the thumbs-up button to mark it as the correct solution.
Thanks & Regards,
Abbas Shaik

Amit Gujarathi
Giga Sage
Giga Sage

HI @Ashok32 ,
I trust you are doing great.

Below is a basic outline of the script you can use in ServiceNow.

// Define the endpoint of the third-party service
var thirdPartyEndpoint = 'https://third-party-service/endpoint';

// Set up the request headers
var headers = {
    'Content-Type': '<content-type>',
    'X-MessageType': 'file',
    'X-FileName': '<file-name>',
    'X-MessageId': '<message-id>',
    'X-EntityType': 'incident',
    'X-Action': 'update',
    'X-Integration': '<integration-details>'
};

// Function to fetch attachments from the third-party tool
function fetchAttachments() {
    var client = new sn_ws.RESTMessageV2();
    client.setEndpoint(thirdPartyEndpoint);
    client.setHttpMethod('GET');

    // Add headers to the request
    Object.keys(headers).forEach(function(key) {
        client.setRequestHeader(key, headers[key]);
    });

    // Send the request and get the response
    var response = client.execute();
    var responseBody = response.getBody();
    var httpStatus = response.getStatusCode();

    if (httpStatus == 200) {
        // Process and upload attachments to ServiceNow
        uploadAttachmentsToServiceNow(responseBody);
    } else {
        // Handle errors
        gs.error('Error fetching attachments: ' + httpStatus);
    }
}

// Function to upload attachments to a ServiceNow incident
function uploadAttachmentsToServiceNow(attachments) {
    // Assume 'attachments' is the parsed data from the third-party tool
    // Loop through each attachment and upload it to the ServiceNow incident

    // For demonstration,

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



Hi  @Amit Gujarathi

The above code seems to be good could you please provide me the code to upload attachments to a ServiceNow incident.