Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Not getting sys id of the attachment when using g_form.getValue

EKTA2
Tera Expert

Hi. I need to grab the sys id of the submitted Attachment variable so that I can push the same image ( submitted by the user) to sc_cat item table. 

 

I am not getting the sys_id of the attachment variable. It is coming blank when I am using getValue method. pls suggest. 

17 REPLIES 17

Vamsi Sreenivas
Tera Guru

HI, Attachments get stored in sys_attachment table. You need to gliderecord to that table and get that sys_id. You cannot access it using g_form.

Can you share the script and the scenario that you need the sys_id of attachment.

 

Regards,

Vamsi S

Hi Vamsi, 

The Scenario is that when a user submits an Icon and a Picture ( I am using attachment variable for both) to be updated for a catalog item, I need to automatically update that icon and picture in the respective catalog Item. 

 

So what I wanted to do was:

 

1. to grab the sys_ id of the icon variable 

2. to grab the sys_id of picture variable

3. To Gliderecord sys_attachment for exact sys_ids against table_id 

4. And then push both of these to icon and picture in the sc_cat_item. I need to be able to identify icon and picture separately so that icon can be pushed to icon and picture to picture.

 

Setup is something like below. 

 

find_real_file.png

@EKTA , Try the MODIFIED below script:  place this script in server side

var catIcon = '';
var catPicture = '';

var grAttachment = new GlideRecord('sys_attachment');
grAttachment.addEncodedQuery("sys_id="+YOUR_ICON_VARIABLE_VALUE); // Value of ICON variable of the record user submits
grAttachment.query();
if(grAttachment.next()) {
    var grNew = new GlideRecord('sys_attachment');
    grNew.initialize();
    grNew.file_name = grAttachment.file_name;
    grNew.table_name = 'ZZ_YY' + 'sc_cat_item'; // Replace sc_cat_item with sc_cat_item_producer if your catalog item is sc_cat_item_producer
    grNew.table_sys_id = SYS_ID_OF_CATALOG_ITEM_USER_SELECTED;
    grNew.content_type = grAttachment.content_type;
   catIcon = grNew.insert();
}

grAttachment = new GlideRecord('sys_attachment');
grAttachment.addEncodedQuery("sys_id="+YOUR_PICTURE_VARIABLE_VALUE); // Value of ICON variable of the record user submits
grAttachment.query();
if(grAttachment.next()) {
    grNew = new GlideRecord('sys_attachment');
    grNew.initialize();
    grNew.file_name = grAttachment.file_name;
    grNew.table_name = 'ZZ_YY' + 'sc_cat_item'; // Replace sc_cat_item with sc_cat_item_producer if your catalog item is sc_cat_item_producer
    grNew.table_sys_id = SYS_ID_OF_CATALOG_ITEM_USER_SELECTED;
    grNew.content_type = grAttachment.content_type;
   catPicture = grNew.insert();
}

var grCat = new GlideRecord('sc_cat_item');
grCat.get("SYS_ID_OF_CAT_ITEM");
grCat.icon = catIcon;
grCat.picture = catPicture;
grCat.update();

 

Let me know if you have any questions on above script.

Mark my answer as HELPFUL / CORRECT if this help resolve your issue.

Regards,

Vamsi S

Hi Vamsi, 

 

Why are we gliderecording twice ?? Also I am not getting sys_id of picture /icon variable for some reason? It is coming blank when I do get Value. All I have is display Value

grAttachment = new GlideRecord('sys_attachment');
grAttachment.addEncodedQuery("sys_id="+YOUR_PICTURE_VARIABLE_VALUE); // Value of ICON variable of the record user submits
grAttachment.query();
if(grAttachment.next()) {
    grNew = new GlideRecord('sys_attachment');
    grNew.initialize();

 

Cant we skip 2nd gliderecord and just initialize directly?