How to create an incident along with adding an attachment via Rest API post call
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-09-2024 04:48 AM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-09-2024 11:23 AM - edited 08-09-2024 11:23 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-10-2024 08:03 AM
Hi @Sheldon Swift - I am using scripted API, not table api
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2024 10:21 PM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-12-2024 05:22 AM
Hi @srinivasrao - I see a lot of potential issues here. Let's see if we can address them one by one...
- 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.
- 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.
- 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).
- 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.
- 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-12-2024 10:29 AM
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.
"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"
}
]
}