- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-10-2017 09:10 AM
Hi Team,
I am working with one requirement, Which is asking to check the attachment before submitting a "change request" by using record producers.
I have the below two variables,
1. Script information(SI) with choice yes/no
2. Backout script information(BSI) with choice yes/no
If SI is yes, I need to make sure the attachment is added, else it won't allow me submit the request
If BSI is yes, I need to make sure the attachment is added, else it won't allow me submit the request
Now i need to make sure two attachments are added in the request before submitting the request, else it won't allow me submit the request.
If either one of them is yes, I am aware that, I can use the below script to check for attachment.
function onSubmit() {
//Type appropriate comment here, and begin script below
var cat_id = gel('sysparm_item_guid').value;
var s = g_form.getValue('si');
//alert("Script Info is "+s);
if(s == 'yes')
{
var attachment = new GlideRecord("sys_attachment");
attachment.addQuery("table_name", "change_request");
attachment.addQuery("table_sys_id", cat_id);
attachment.query();
if (!attachment.next()) {
alert("Please attach Script information");
return false; }
}
}
How to validate my case? If anyone is having an idea please share with me.
Thanks,
Dinesh C
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-11-2017 02:15 AM
Thanks you all for your responses. I have accomplished that by using the below script.
function onSubmit() {
//Type appropriate comment here, and begin script below
var cat_id = gel('sysparm_item_guid').value;
var rowcount=0;
var s = g_form.getValue('si');
var bs = g_form.getValue('bsi');
//alert("si "+s);
//alert("bsi "+bs);
if((s == "Attach a file") && (bs == "Attach a file"))
{
//alert("pass in S & BS if");
var attachment = new GlideRecord("sys_attachment");
attachment.addQuery("table_name", "change_request");
attachment.addQuery("table_sys_id", cat_id);
attachment.query();
while (attachment.next()) {
rowcount++;
}
//alert("Row "+rowcount);
if(rowcount == 2)
{
return true;
}
else
{
alert("Please attach Script information");
return false; }
}
else if(s == "Attach a file")
{
//alert("pass in S if");
var attachment = new GlideRecord("sys_attachment");
attachment.addQuery("table_name", "change_request");
attachment.addQuery("table_sys_id", cat_id);
attachment.query();
while (attachment.next()) {
rowcount++;
}
//alert("Row "+rowcount);
if(rowcount == 1)
{
return true;
}
else
{
alert("Please attach Script information");
return false;
}
}
else if(bs == "Attach a file")
{
//alert("pass in BS if");
var attachment = new GlideRecord("sys_attachment");
attachment.addQuery("table_name", "change_request");
attachment.addQuery("table_sys_id", cat_id);
attachment.query();
while (attachment.next()) {
rowcount++;
}
//alert("Row "+rowcount);
if(rowcount == 1)
{
return true;
}
else
{
alert("Please attach Backout script information");
return false;
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-28-2021 01:09 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);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-28-2021 01:12 PM
Hi,
Please create your own question so the conversation can be tracked appropriately.
You're posting this across numerous other previously closed/answered threads.
Over 6+ threads at this point.
Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!