Load attachment to RITM through Scripted REST API

AnilM99
Tera Expert

Hi Team,

I am creating RITM's using Scripted REST API, i have a variable called 'upload Photo' (Attachment).

We get payload from oracle which i need to populate that variable in ServiceNow. Other variables are working but the issue is with Attachment variable.

 

Thanks

Anil

 

 

1 REPLY 1

sumanta pal
Kilo Guru

Creating RITM's (Requested Item) using Scripted REST API and handling attachments can be a bit tricky. Here's a step-by-step guide on how you can handle this: 1. First, you need to understand that attachments in ServiceNow are not stored as a field on a record. They are stored in a separate table called sys_attachment. This means you cannot directly populate an attachment field in a record. 2. When you receive the payload from Oracle, you need to ensure that the attachment data is in a format that ServiceNow can understand. Typically, this would be a Base64 encoded string. 3. In your Scripted REST API, you need to handle the attachment separately from the other fields. You can use the AttachmentCreator API to create an attachment from the Base64 string. Here's a sample code snippet: javascript var gr = new GlideRecord('sc_req_item'); gr.initialize(); gr.short_description = 'Test RITM'; gr.insert(); var attachment = new GlideSysAttachment(); var decodedData = GlideStringUtil.base64Decode('your_base64_string'); attachment.write(gr, 'filename.jpg', 'image/jpeg', decodedData); In this code: - A new RITM is created with a short description of 'Test RITM'. - The GlideSysAttachment API is used to create a new attachment. - The Base64 string is decoded into a byte array using the GlideStringUtil.base64Decode method. - The write method is used to attach the decoded data to the RITM record. 4. After the attachment is created, it will be linked to the RITM record, but it will not be visible in any specific field. You can view it in the 'Attachments' related list on the RITM form. 5. If you want to show a link to the attachment in a specific field, you can create a UI Script or Client Script to generate a URL to the attachment and populate that in a field. Remember: - Always ensure that the payload you receive from Oracle is correctly formatted and the Base64 string is valid. - Test your API with different types of attachments to ensure it works correctly. - If you encounter any issues, use the gs.info or gs.debug methods to log information to the system log for debugging.