Attachment on Catalog task

Vaishali 11
Tera Guru

I want to make attachment mandatory on a particular catalog task for only one catalog item. How can I achieve it?

 

Thanks.

Vaishali

1 ACCEPTED SOLUTION

If you're getting the alert even if the attachment is there, then something's not working with the GlideRecord, which most likely comes back to the fact that it's running in a client script.  Here's what a Catalog Client Script would like with a Glide Ajax:

function onSubmit() {
    var st = g_form.getValue('state');
    var sd = g_form.getValue('short_description');
    if (sd == 'Fulfillment Task') { //to only run this for one particular Catalog Task
        if (st == 3) { //to only run this when the State is Closed Complete
            var sysid = g_form.getUniqueValue();
            var ga = new GlideAjax('CheckAttachment');
            ga.addParam('sysparm_name', 'taskAttached');
            ga.addParam('sysparm_sysid', sysid);
            ga.getXMLWait();
            var answer = ga.getAnswer();
            if (answer == 'false') {
                alert("You must attach the required forms prior to closing this task");
                return false;
            }
        }
    }
}

Then your Script Include with the Client callable box checked would look like this:

var CheckAttachment = Class.create();
CheckAttachment.prototype = Object.extendsObject(AbstractAjaxProcessor, { 
	
	taskAttached: function (){
		var answer = 'false';  
		var id = this.getParameter('sysparm_sysid');
		var attachment = new GlideRecord('sys_attachment');
		attachment.addQuery('table_name', 'sc_task');
		attachment.addQuery('table_sys_id', id);
		attachment.query();
		if(attachment.next()){
			answer = 'true';
		}
	return answer;
	},

type: 'CheckAttachment'
});

View solution in original post

11 REPLIES 11

Amitoj Wadhera
Kilo Sage

 Hello Vaishali,

 

 

Write a "Business rule before update" on sc_task table.

 

Script:

if (!(current.hasAttachments() == true))  {
          gs.addErrorMessage('Attachment is mandatory');  
          current.setAbortAction(true);  

}

This Business Rule would prevent a Catalog Task from being created if it doesn't have an attachment.

Vaishali 11
Tera Guru

For the above requirement, I have created this script, but it's giving the pop-up even if the attachment is there :-

function onSubmit() {
var cat_id = g_form.getUniqueValue();


var gr = new GlideRecord("sys_attachment");


gr.addQuery("table_name", "sc_cart_item"); //"sc_cart_item"


gr.addQuery("table_sys_id", cat_id); // cat_id

gr.query();

if (gr.next()) {

alert("Please attach the required Central Express Survey before closing the task.");return false;}
}

Any updates on this?

GlideRecords in a client script are not advisable/supported, so technically you should change this to a GLideAjax call to a script include.  In any event, the query is looking for a record on the attachment table that matches the current request, then alerting if a record is found, so you'll want to instead use

if(!gr.next()) {

Could you please share the snippets of script include and client script.