How can i get the atttachments sys_id before Submit a form?

PereR
Tera Contributor

I have been asked to check the attachment header of a request.

 

I have tried using an attachment variable and got it to work. I have a client script that takes the sys-id of the attachment and calls an include script that checks the format and hedears with a GlideSysAttachment();

 

The problem is that I am asked to do this without the attachment variable and instead with the 'paperclip' attachments. And I don't know how to get the sys-id of these attachments.

 

With the simplemtente variable I can get it with this:
var attachmentSysID = g_form.getValue('attachment');

 

But I have no idea how to do it without the variable. Does anyone have any idea how to do this?

1 ACCEPTED SOLUTION

Nick Parsons
Mega Sage

I'm not too clear on what exactly you're after - the attachment sys_ids or the record that the attachment will be added to, so here's how to get both in a client script. Noting that both of these solutions require inspecting the DOM, so there is a small dependency on this and thus not recommended, but I don't believe there is another way without it

Get an array of attachments

You can get an array of attachments in a catalog client script (running on the portal) by inspecting the angular scope like so:

var $scope = this.angular.element("#sc_cat_item").scope();
var attachmentSysIds = $scope.attachments.map(function(attachmentDetails) {
  return attachmentDetails.sys_id;
});
// Result is an array of sys_attachment sys_ids: 
// ["5cf0d22453b9121036a838f0a0490e55", ...]

Get the sys_id of the to be record

You can get the sys_id of the record that's about to be generated by also looking at the angular scope, but at different properties (this is for catalog client scripts running on the portal):

var $scope = this.angular.element("#sc_cat_item").scope();
var toBeTableName = $scope.data._attachmentTable;
var toBeSysId = $scope.data._generatedItemGUID;
// Result is the record the Record Producer is going to create, for Catalog Items, this is the `sc_cart_item` record:
// toBeTableName = incident 
// toBeSysId = 2943522053f9121036a838f0a0490e78
// You can query the sys_attachment table's `table_name` and `table_sys_id` fields that match the above to find the attachments uploaded.

 

View solution in original post

12 REPLIES 12

palanikumar
Mega Sage

You can create a Client script like below. Move the GlideRecord script to GlideAjax:

var sysid = g_form.getUniqueValue();
var attach = new GlideRecord('sys_attachment');
attach.addQuery('table_sys_id',sysid);
attach.query();
while(attach.next()){
  alert("Attachment sys_id" + attach.sys_id);
}

 

Thank you,
Palani

I had already tried it and it does not work, g_form.getUniqueValue() returns the sysid of the catalog item which is always the same, in my case 577969f183ad96508d0cb100feaad37f. On the contrary every time I make the request a different id in the variable table sys id.

PereR_0-1730965627055.png
I've also searched with the snutils this sys id in a gobal search of all the intance and it doesn't find anything.

jcmings
Mega Sage

I've been trying to figure this out for a little bit now... I believe you'll want to find the GUID but I don't know if that's possible in Service Portal. Here's a post where someone got the sysparm_item_guid value (but I believe it was done in Platform view; I got console errors when trying it on Portal).

 

Also there is another user who is trying to do something similar -- might be worth following their thread: link

Hitoshi Ozawa
Giga Sage
Giga Sage

@PereR It's not possible. When an attachment is uploaded, it will create a record in the sys_attachment table but no record is created in sc_cart_item table. By looking at the "Created" date/time, it's possible to find the file if only 1 user was filling the form, but if there were several users simulatenously adding attachments, it's not possible to find which user the attachment was made from.

FYR:

https://www.servicenow.com/community/csm-forum/get-attachment-sys-id-before-catalog-item-is-submitte...

Nick Parsons
Mega Sage

I'm not too clear on what exactly you're after - the attachment sys_ids or the record that the attachment will be added to, so here's how to get both in a client script. Noting that both of these solutions require inspecting the DOM, so there is a small dependency on this and thus not recommended, but I don't believe there is another way without it

Get an array of attachments

You can get an array of attachments in a catalog client script (running on the portal) by inspecting the angular scope like so:

var $scope = this.angular.element("#sc_cat_item").scope();
var attachmentSysIds = $scope.attachments.map(function(attachmentDetails) {
  return attachmentDetails.sys_id;
});
// Result is an array of sys_attachment sys_ids: 
// ["5cf0d22453b9121036a838f0a0490e55", ...]

Get the sys_id of the to be record

You can get the sys_id of the record that's about to be generated by also looking at the angular scope, but at different properties (this is for catalog client scripts running on the portal):

var $scope = this.angular.element("#sc_cat_item").scope();
var toBeTableName = $scope.data._attachmentTable;
var toBeSysId = $scope.data._generatedItemGUID;
// Result is the record the Record Producer is going to create, for Catalog Items, this is the `sc_cart_item` record:
// toBeTableName = incident 
// toBeSysId = 2943522053f9121036a838f0a0490e78
// You can query the sys_attachment table's `table_name` and `table_sys_id` fields that match the above to find the attachments uploaded.