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

SanjivMeher
Kilo Patron
Kilo Patron

It should be a onBefore Business rule instead of a client script.

And you need to use current.setAbortAction(true); to stop the update

 

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


var sysid =current.getValue('sys_id');

//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)
gs.addErrorMessage("Attachment Required!!");
current.setAbortAction(true);
} else if (rowcount == 1 && attachment.content_type.indexOf("cip") <= -1) {

gs.addErrorMessage("Please attach CIP file");
current.setAbortAction(true);

}


Please mark this response as correct or helpful if it assisted you with your question.

Filipe Cruz
Kilo Sage
Kilo Sage

Hi KeithM,

If you do that in a Client script, you should not use the GlideRecord, since that is a server side object.
You need to perform a GlideAjax call to the server to retrieve the data.

One thing that you can do that will make your implementation easier is to use a before update Business rule.
That business rule will perform the query to the sys_attachment table and, in case there is no file or the file name is not the right one, you can use the following statement to abort the save:

current.setAbortAction(true);

And in addition you can throw an error message to your user with information about the missing attachment: (put this one before the setAbortAction statement)

gs.addErrorMessage("The attachment xpto is missing!");

So I would say your before update business rule code can look like:

(function executeRule(current, previous /*null when async*/) {	
	
	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.query();
	while(attachment.query()){
		hasAttachment = true;
		if(attachment.getValue("content_type").indexOf("cip") > -1){
			hasCIPFile = true;
			break;
		}
	}
	
	if(!hasAttachment)
		gs.addErrorMessage("Attachment Required!");
	
	if(!hasCIPFile)
		gs.addErrorMessage("Please attach CIP File!");
	
	if(!hasAttachment || !hasCIPFile)
		current.setAbortAction(true);
	
})(current, previous);

I did not test this code, only wrote them in notepad++ and copied it to here, so you might need to have a better look to see if you agree with that. But the main idea is there! 

Hope this helps you!

Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!

Best Regards,

Filipe Cruz

 

 

Filipe,

This has been super helpful!!  I am running into an issue though.  When I do attach a file that contains "CIP" in it, I keep getting the error message to attach the CIP file.  Any thoughts or ideas?

Hello KeithM,

Is the file content_type equals to "cip"? 
The validation that I posted in the code is based on the content type. You can change that validation to look into the filename and check if the word "CIP" is there.

Is this helpful?

Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!

Best Regards,

Filipe Cruz