Attachment mandatory in catalog task when specific user select

Sirri
Tera Guru

Hi All,

 

In the catalog item  variable is Risk Owner (risk_owner) it's type is Reference(sys_user).

 

when we select any user. If that user grade is less than Director at that time only Attachment want to be mandatory.

Note: Grade(single Line text) is column from user(sys_user) table.

 

please see attachment for your reference. when I select that user only  at that time only attachment mandatory.

 

Any source code available please share.

 

6 REPLIES 6

Satyapriya
Mega Sage

Hi @Sirri,

 You can write an onchange client script for that By using the GlideAjax() you can fetch the server side value like 

if the current user has grade value less than the director then you can write in the client script like 

if(answer==true && this.document.getElementByClassName('get-attachment').length == 0){

g_form.addErrorMessage('Attachment is mandatory');

}

Please Mark it as solution or helpful if this is helpful for you.

Regards,

Satyapriya

Hi Priya,

I tried but they showing answer is not defined in catalog client script. Please find the attachment for your reference

@Sirri In order to resolve the error, you need to update your script as follows.

function onSubmit() {
//Type appropriate comment here, and begin script below 
var risk = g_form.getValue('risk_owner');
var ga = new GlideAjax ('GradelessthanDirector');
ga.addParam('sysparm_name', 'grade'); 
ga.addParam('sysparm_value', risk); 
ga.getXML (populate);
function populate (response) {
var answer = response.responseXML.documentElement.getAttribute("answer"); 
alert(answer);
if (answer==true && this.document.getElementByClassName('get-attachment').length == 0) {
g_form.addErrorMessage('Attachment is mandatory');
}
}

If the code fails at line 

this.document.getElementByClassName('get-attachment').length

You need to insure that Isolate Script checkbox on your client script form is unchecked.

 

Also, since you are making an asynchronous call from your onSubmit script, it is quite possible that the form get submitted before you even get a response from the server, to avoid this follow the approach mentioned on this link  https://www.servicenow.com/community/developer-blog/how-to-async-glideajax-in-an-onsubmit-script/ba-... to make sure that the attachments get validated before the form is submitted else error is shown and the form submission is stopped.

 

Hope this helps.

 

@Sandeep Rajput 

 

I have tried but I'm not getting Please can you share the entire code it will helpful for me.