
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2020 03:42 AM
Hello All,
I have a requirement to copy attachment from existing record and populate it on new Catalog item in service portal.
For example, lets say there is one incident and below that incident number, there is a link to catalog item. When user opens that link, all the attachments in the incident should be populated on the catalog item form.
Then user can remove/attach new attachments and submit form.
Thanks and Regards,
Ali
Thank you,
Ali
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2020 07:56 AM
Hello Jaspal,
Thank you for your response.
I was able to fulfill my requirement by reading unique GUID generated on catalog item widget and by copying records to that sys_id.
I am using record producer, hence the attachments are added with actual table name in attachments table and actual sys_id (which will be generated as soon as the form is loaded in portal, same I am reading in my script and using to copy attachments).
Thank you,
Ali
Thank you,
Ali

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2020 07:56 AM
Hello Jaspal,
Thank you for your response.
I was able to fulfill my requirement by reading unique GUID generated on catalog item widget and by copying records to that sys_id.
I am using record producer, hence the attachments are added with actual table name in attachments table and actual sys_id (which will be generated as soon as the form is loaded in portal, same I am reading in my script and using to copy attachments).
Thank you,
Ali
Thank you,
Ali
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-13-2023 01:18 AM
Hello @Ahmmed Ali ,
I have a requirement for which I need to read the unique GUID or the cart item id on the catalog item widget when the catalog item form loads. Could you please tell me how you read the GUID on service portal?
Thank you!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2020 04:04 AM
Hi
You can use the GlideSysAttachment.copy method to copy the attachments from one record to another.
GlideSysAttachment.copy('sourcetable','sys_id','destinationtable','sys_id');
Refer: https://docs.servicenow.com/bundle/orlando-servicenow-platform/page/script/useful-scripts/reference/...
If you want the attachments on the catalog item to be in sync with the attachments on the incident, i.e. if the attachment is deleted on the incident, then if you want to delete it from the catalog item as well, then you can probably write a business rule that runs on insert, update and delete, on the sys_attachment table, filtering on the 'table_name' field for incident attachments, and on the 'table_sys_id' for the current.sys_id, and then copies/deletes the current attachment record from the catalog item record as well.
In order to display the attachments on the portal page, and have the users to be able to add new ones and delete the existing ones, you can use the <sp-attachment-button> and <sp-attachment-manager> directives, that help you insert new attachments, and edit/delete the existing ones.
If you'd take a look at the OOTB form widget, it has the code to deal with the attachments, using the above mentioned directives.
Thanks & Regards,
Rishabh Jha
Aavenir (https://www.aavenir.com/)

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2020 05:37 AM
Hi Rishabh,
Thank you for response.
Yes, If any record was getting created then I would have used GlideSysAttachment() API at server side. But here since catalog item is not yet submitted and there is no record in server side for the item, I am not able to figure it out how to do it.
Please suggest.
Thanks,
Ali
Thank you,
Ali
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-28-2021 01:05 PM
HI everyone,
I have a requirement in which we need to check first check the mandatory attachment in serviceportal, which could be done OOB now. Along with that we need to check whether the attachment is excel or not , and if it is excel we need to count the filled rows in it. Any help will be appreciated. I have the code for checking whether the attachment is an excel or not, but its not working in portal as we are using Dom manipulation there . Below is the working code in the Service-now console and also i have the code which counts the excel rows but not able to achieve in portal.. Below are both the codes.
Excel mandatory working in console but not in portal:
Client script
function onSubmit() {
var cat_id = sysparm_item_guid.value;
var attObj = new GlideRecord('sys_attachment');
attObj.addQuery('table_name', 'sc_cart_item');
attObj.addQuery('table_sys_id', cat_id);
attObj.query();
if (attObj.hasNext()) {
while (attObj.next()) {
var typeMatch = 'ms-excel,spreadsheetml';
var typeStr = attObj.content_type.toString();
var typeChk = (typeStr.indexOf(typeMatch[0]) > -1 || typeStr.indexOf(typeMatch[1]) > -1);
var fName = attObj.file_name.toString();
var regex = /\.xlsx$/g;
var extChk = regex.test(fName);
if(!typeChk || (typeChk && !extChk)) {
alert("You must use an Excel spreadsheet for uploading, please remove the existing file and re-attach");
return false;
}
}
}
}
Counting nunmber of rows
Background script
var count=0;
var parser = new sn_impex.GlideExcelParser();
var attachment = new GlideSysAttachment();
// use attachment sys id of an excel file attachment
var attachmentStream = attachment.getContentStream('6e9c7aad1b772850c1534159cc4bcb5d'); //sys_id of record from sys_attachment table
parser.parse(attachmentStream);
//retrieve the column headers
var headers = parser.getColumnHeaders();
gs.print("Apoorva"+headers );
var key = headers[0];
var value = headers[1];
while(parser.next())
{
count++;
var row = parser.getRow();
//print row value for both columns
gs.print(row[value]) ; //Uncomment this to get actual data.
}
gs.print('Number of Rows in excel attached are '+count);