How to create an incident along with adding an attachment via Rest API post call

srinivasrao
Tera Contributor

Hi Team,

Team,

I wrote a scripted API and trying to post the data from rest API explored, but i dont see provision to add attachement data from explorer to test the case.

May i know how to achive this?

 

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

// implement resource here
var requestBody = request.body.data;
var incidentGR = new GlideRecord('incident');
incidentGR.initialize();

// Set fields without inserting the record yet
incidentGR.short_description = requestBody.short_description;
incidentGR.caller_id.setDisplayValue(requestBody.caller_id);
incidentGR.short_description = requestBody.short_description;
incidentGR.caller_id.setDisplayValue(requestBody.caller_id);

// Attachments processing (simulated for now)
// Assuming attachments are part of the request payload
var attachmentIDs = [];
if (requestBody.attachments && requestBody.attachments.length > 0) {
for (var i = 0; i < requestBody.attachments.length; i++) {
var attachment = requestBody.attachments[i];
var attachmentGR = new GlideRecord('sys_attachment');
attachmentGR.initialize();
attachmentGR.table_name = 'incident';
attachmentGR.table_sys_id = incidentGR.sys_id;
attachmentGR.file_name = attachment.file_name;
attachmentGR.content_type = attachment.content_type;
attachmentGR.insert();
attachmentIDs.push(attachmentGR.sys_id);
}
}

// Now insert the incident record
// Return response with incident and attachment info
response.setBody({
sys_id: incidentSysID,
number: incidentGR.number,
attachments: attachmentIDs
});
})(request, response);
17 REPLIES 17

Hi @srinivasrao - I haven't seen the Table API used in this way. Have you considered using the Table API to create the incident first, then passing the sys_id from the response body to the Attachment API to attach the file? Just keep in mind that if you go that route the Attachment API expects a binary payload, so you'll need to decode the Base64 encoded string.

 

Alternatively, you could use GlideSysAttachment's writeBase64 method.

Hi @Sheldon  Swift  - I am using scripted API, not table api 

Team ,

Now have changed the design from scripted rest api to import set API, but the code in the onAFTER transform script not working, and attachment not creating. Can you please help?

(function runTransformScript(source, map, log, target) {
// Check if attachment data is present
if (gs.nil(source.u_content) || gs.nil(source.u_fileName) || gs.nil(source.u_contentType)) {
// If any attachment field is missing, skip attachment processing
log.info('Attachment data is incomplete. Skipping attachment.');
return;
}

// Retrieve the incident record using the target sys_id
var inc = new GlideRecord('incident');
if (inc.get(target.sys_id)) {
// Create an attachment
var attachment = new GlideSysAttachment();
 
// Attach the file to the incident record
var attachmentSysId = attachment.write('incident', inc.sys_id, source.u_file_name, source.u_content_type, source.u_content);
 
if (attachmentSysId) {
log.info('Attachment added successfully: ' + source.u_file_name);
} else {
log.warn('Failed to add attachment: ' + source.u_file_name);
}
} else {
log.warn('Incident record not found: ' + target.sys_id);
}
})(source, map, log, target);

Hi @srinivasrao - I see a lot of potential issues here. Let's see if we can address them one by one...

 

  1. Variable naming
    • source.u_fileName is checked initially, but source.u_file_name is used later.
    • source.u_contentType is checked initially, but source.u_content_type is used later.
  2. write(GlideRecord record, String fileName, String contentType, String content)
    • Looking at the method signature, you're not passing the correct parameters. In this case,  you would pass target (which is a GlideRecord object) as the first parameter.
  3. Content encoding
    • source.u_content needs to be in a proper binary format, not a Base64 encoded string. If the content is Base64 encoded, you need to decode it before attaching it (otherwise, use GlideSysAttachment's writeBase64 method as suggested earlier).
  4. Field types and validations
    • Ensure that the fields u_content, u_file_name/u_fileName and u_content_type/u_contentType are populated correctly in your import set table.
  5. Logging
    • Add more detailed logging to help diagnose where the process might be failing.

 

Additionally, I suggest using "Insert/Edit code sample" when posting code here.

Hi @Sheldon  Swift ,

 

Thanks for you mail with full analysis:

Please find below the rectifications i did after your observations.

1. I have source.u_file_name, source.u_content_type as per the importset fields created and changed them accordingy while check the attachment

2. I modified the write statment.

3. I have double checked the import set attachement fields created

4. Its not going to onAfter script after creation of incident it seems. I checked the logs for the attachment details which are not created as given below in the code in red.

 

// Check if attachment data is present
if (gs.nil(source.u_content) || gs.nil(source.u_file_name) || gs.nil(source.u_content_type)) {
// If any attachment field is missing, skip attachment processing
log.info('Attachment data is incomplete. Skipping attachment.');
return;
}
log.info('content type:' + source.u_content + ' file name' + source.u_file_name + ' Content type: '+ source.u_content_type)
// Retrieve the incident record using the target sys_id
var inc = new GlideRecord('incident');
if (inc.get(target.sys_id)) {
// Create an attachment
 
**********************************************
Sending the sample data posting from Rest API call.
{
"beneficiary": "6901",
"short_description": "Issue with mobile device",
"description": "Issue with mobile device",
"level1": "iphone",
"level2": "Charging",
"attachments": [
{
"fileName": "example.txt",
"contentType": "text/plain",
"content": "VGhpcyBpcyBhIHNhbXBsZSBmaWxlIGNvbnRlbnQu"
}
]
}
var attachment = new GlideSysAttachment();
 
try {
// Attach the file to the incident record
var attachmentSysId = attachment.write(inc.sys_id, source.u_file_name, source.u_content_type, source.u_content);
 
if (attachmentSysId) {
log.info('Attachment added successfully: ' + source.u_file_name);
} else {
log.error('Failed to add attachment: ' + source.u_file_name);
}
} catch (e) {
log.error('Error during attachment creation: ' + e.message);
}
} else {
log.warn('Incident record not found: ' + target.sys_id);
}