getting sys_id of currently open catalog form
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-10-2024 03:36 AM
Hi Everyone,
I have a catalog item, and due to some organization standards I cannot use variable type 'attachment'. so I had to go ahead with form attachment. now that I want to clear the attachments based on some onchange conditions, Im not able to find a solution for that. I have referred THIS but it doesnt serve the purpose on service portal(it works only through 'try it' button).
is there any other way that I can access the 'sys_id' of currently open catalog form(when I attach an attachment, the sys_id of currently open form is pasted in sys_attachment table). using this sys_id I want to enhance user experience by deleting the attachment onchange of relevant fields.
I really appreciate any solution for this usecase,
thanks and regards,
Ubada Barmawar.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-10-2024 04:05 AM
@Ubada Barmawar Attachments uploaded in the Service Portal are temporarily linked using a temporary ID (e.g., sysparm_item_guid) until the form is submitted. You can access this ID in your client script using:
var tempId = g_form.getParameter('sysparm_item_guid');
This tempId can be used to query the sys_attachment table to get attachments associated with the form before submission.
Example:
var ga = new GlideAjax('AttachmentHandler');
ga.addParam('sysparm_name', 'getAttachments');
ga.addParam('sysparm_temp_id', tempId);
ga.getXMLAnswer(function(response) {
var attachments = JSON.parse(response);
// Process attachments as needed
});
The Script Include (AttachmentHandler) could look like this:
var AttachmentHandler = Class.create();
AttachmentHandler.prototype = {
initialize: function() {},
getAttachments: function(tempId) {
var result = [];
var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_sys_id', tempId);
gr.query();
while (gr.next()) {
result.push({
sys_id: gr.getValue('sys_id'),
file_name: gr.getValue('file_name')
});
}
return JSON.stringify(result);
},
type: 'AttachmentHandler'
};
Once you have the list of attachments, you can delete them based on specific conditions using the GlideSysAttachment API in a GlideAjax call:
var ga = new GlideAjax('AttachmentHandler');
ga.addParam('sysparm_name', 'deleteAttachments');
ga.addParam('sysparm_temp_id', tempId);
ga.getXMLAnswer(function(response) {
// Handle deletion respon
se
});
hope this will help you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-10-2024 07:21 AM
unfortunately, g_form.getParameter('sysparm_item_guid') doesnt seem to work on sp, but it does work in native UI.
so..can we make it work somehow?