Help with writeBase64 in GlideSysAttachment
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-05-2024 09:14 PM - edited 08-05-2024 10:59 PM
Hi Everyone,
I am trying to send base64 image as attachment to sys_cs_collab_chat table using GlideSysAttachment , but it not attaching and not even returning any error.
Scripted Rest API:
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
// implement resource here
var requestBody = request.body.dataString;
var parser = new global.JSON();
var parsedData = parser.decode(requestBody);
var imageName = parsedData.imageName;
var imageType = "image/jpeg";
var imageValue = parsedData.imageValue;
var table = parsedData.object_name;
var sysId = parsedData.object_id;
var rec = new GlideRecord(table);
rec.addQuery('sys_id', sysId);
rec.query();
if (rec.next()) {
if (imageName && imageValue && imageType != '') {
var sa = new GlideSysAttachment();
sa.writeBase64(rec, imageName, imageType, imageValue);
}
}
})(request, response);
JSON payload;
{"object_id":"ea09239a97d00290c0ebd404a253af08",
"imageName":"QjjeAKNbCwNoJwEKrg1dObUx.jpg",
"imageValue":"<<base64FileContent>>",
"object_name":"sys_cs_collab_chat"}
Can anyone point out my mistake.
cc: @Ankur Bawiskar
Thanks in advance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-06-2024 01:00 AM
are you in scoped app?
Did you check the script is going inside the if statement where the GlideSysAttachment code is present?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-06-2024 01:41 AM
Hi @Ankur Bawiskar ,
No, it is in global scope. In code I am using:
var sa = new global.GlideSysAttachment();
and it is going inside the if loop of GlidesysAttachment, I placed gs.info in loop and checked it.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-06-2024 02:01 AM
writeBase64 won't work in global scope. that method is only for scoped app.
you need to use SOAP Attachment creator for this
if (imageName && imageValue && imageType != '') {
var ecc = new GlideRecord('ecc_queue');
ecc.initialize();
ecc.agent = "AttachmentCreator";
ecc.topic = "AttachmentCreator";
ecc.name = imageName + ":" + imageType;
ecc.source = table + ":" + rec.sys_id;
ecc.payload = imageValue;
ecc.insert();
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-06-2024 02:11 AM
I changed it into Scoped application. Now I can see it in attachment table. But I am not able to see it in sidebar chat.
Thanks