How to check attachment name in a change request

KeithM1
Tera Expert

I've got a need to check and make sure that a file attachment contains a certain name for Standard changes when the change moves to the scheduled state.  So when the standard change moves to Scheduled, the script needs to check and make sure that there is a file attached to it that contains the letters CIP in the name.  If there is none or if there is no attachment, it needs to stop the update.  Here's the client script I have:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}

if ((g_form.getValue('type' == 'standard')) && (g_form.getValue('state' == -2))) {


var sysid = g_form.getElement('sysparm_item_guid').value;

alert('sysid is ' + sysid); // is this coming properly


var attachment = new GlideRecord("sys_attachment");
attachment.addQuery("table_name", "change_request");
attachment.addQuery("table_sys_id", sysid);
attachment.query();
var rowCount = attachment.rows.length;
if (rowcount != 1) {
if (rowcount == 0)
alert("Attachment Required!!");
return false;
} else if (rowcount == 1 && attachment.content_type.indexOf("cip") <= -1) {
alert(attachment.content_type);
alert("Please attach CIP file");
return false;
} else return true;

}

}

 

Thoughts/suggestions on what I'm missing (or doing wrong for that matter)?

7 REPLIES 7

I updated the code to this:

if (attachment.getValue("file_name").indexOf("cip") > -1)

 

and I still get the error message to attach a file that contains CIP even if there is one.

I got it figured out.  Here's what the final code looks like:


// var hasAttachment = false;
var hasCIPFile = false;
var attachment = new GlideRecord("sys_attachment");
attachment.addQuery("table_name", "change_request");
attachment.addQuery("table_sys_id", current.sys_id);
attachment.addQuery("file_name", 'CONTAINS', 'cip');
attachment.query();
// while (attachment.query()) {
// hasAttachment = true;

if (!attachment.next()) {
gs.addErrorMessage('Missing CIP File attachment');
current.setAbortAction(true); // Message to be displayed to user
}

 

I appreciate your help with this!

cp11
ServiceNow Employee
ServiceNow Employee

try this:

The alert will work, but if you should not save the record thats a different issue. 

var attachment = new GlideRecord("sys_attachment");
attachment.addQuery("table_name", "incident");
attachment.addQuery("table_sys_id", g_form.getUniqueValue());
attachment.query();
if (attachment.next()) {
if (attachment.content_type.indexOf("cip") <= -1) {
alert(attachment.content_type);
alert("Please attach CIP file");
// return false;
}
}
else {

alert("Attachment Required!!");
//return false;
}