Inbound REST API for receiving attachments and field information to Case table
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-27-2024 10:21 PM
Hi Team,
We are working on SNOW to SNOW integration through REST API between incident data and Case data. Below is the scripted web service code but unable to get attachments and comments data, kindly help for fix:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-27-2024 10:58 PM
- Hi @maneesh3,
Here the code easily you can achieve the snow-to-snow integration,
Your code for integrating attachments and comments between ServiceNow instances using REST APIs has a solid foundation but requires some corrections and refinements to handle attachments and comments properly. Here's the updated and fixed version of your script
(function process(request, response) {
// Parse the request body
var body = request.body.dataString;
var payload = JSON.parse(body);
// Extract key data from the payload
var comments = payload.comments;
var state = payload.state;
var priority = payload.priority;
var short1 = payload.short_description;
var desc1 = payload.description;
var custnum = payload.number;
// Initialize attachment data (if available)
var attachment = payload.attachment || {};
var file_name = attachment.file_name;
var content_type = attachment.content_type;
var base64Data = attachment.data; // Assuming data is base64-encoded content
var correlationID = payload.correlation_id || payload.sys_id;
// Initialize message and case number for response
var message = '';
var number = '';
// Query to find an existing case using correlation_id or sys_id
if (JSUtil.notNil(custnum) && JSUtil.notNil(correlationID)) {
var caseRecord = new GlideRecord('sn_customerservice_case');
caseRecord.addQuery('correlation_id', correlationID);
caseRecord.query();
if (!caseRecord.next()) {
// No existing case, create a new one
caseRecord.initialize();
caseRecord.state = state;
caseRecord.u_genesys_ticket = comments;
caseRecord.priority = priority;
caseRecord.correlation_id = correlationID;
caseRecord.short_description = short1;
caseRecord.description = desc1;
caseRecord.u_customer_ticket = custnum;
caseRecord.insert();
message = 'Case created successfully';
number = caseRecord.number;
} else {
// Existing case found, update it
caseRecord.state = state;
caseRecord.u_genesys_ticket = comments;
caseRecord.priority = priority;
caseRecord.short_description = short1;
caseRecord.description = desc1;
caseRecord.update();
message = 'Case updated successfully';
number = caseRecord.number;
}
// Add comments if available
if (JSUtil.notNil(comments)) {
var journalEntry = new GlideRecord('sys_journal_field');
journalEntry.initialize();
journalEntry.name = caseRecord.sys_id;
journalEntry.element = 'comments';
journalEntry.value = comments;
journalEntry.insert();
}
}
// Handle file attachment (if any)
if (JSUtil.notNil(file_name) && JSUtil.notNil(content_type) && JSUtil.notNil(base64Data) && JSUtil.notNil(correlationID)) {
var caseForAttachment = new GlideRecord('sn_customerservice_case');
if (caseForAttachment.get('correlation_id', correlationID)) {
try {
var sa = new GlideSysAttachment();
var decodedData = GlideStringUtil.base64Decode(base64Data); // Decode base64 content
sa.write(caseForAttachment, file_name, content_type, decodedData);
number = caseForAttachment.number;
message = 'Attachment processed successfully';
} catch (e) {
gs.error('Error attaching file: ' + e.message, 'SNOW to SNOW Integration');
message = 'Error processing attachment';
return sendResponse(number, message, 500);
}
} else {
message = 'Invalid correlation ID for attachment';
return sendResponse(number, message, 400);
}
}
// Prepare response
function sendResponse(number, message, statusCode) {
var responseBody = {
caseNumber: number || '',
message: message || ''
};
response.setStatus(statusCode || 200);
response.setContentType('application/json');
response.setBody(responseBody);
}
// Send the final response
sendResponse(number, message, 200);
})(request, response);
Payload for Testing: Ensure the attachment.data field in the payload contains base64-encoded content. Example:
{
"number": "INC0010001",
"state": "In Progress",
"priority": "High",
"short_description": "Test Incident",
"description": "This is a test",
"comments": "Adding a comment via API",
"correlation_id": "12345",
"attachment": {
"file_name": "test_file.txt",
"content_type": "text/plain",
"data": "dGVzdCBmaWxlIGNvbnRlbnQ="
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-02-2024 02:50 AM
Thank you mahemoodhus, I will connect with other side of SNOW instance and update . Thank you for your support