Make attachment mandatory based on a variable from Catalog Item

Steven Karl
Kilo Contributor

Hi everyone! I'm fairly new to ServiceNow. I'm having difficulties in making the attachment mandatory based from the value of a variable in Catalog Item.

What I wanted to achieve is when I select Create a New Service, attachment becomes mandatory. 

find_real_file.png

On the same catalog item, I was able auto populate the fields based on the Service Name selected using script include and GlideAjax. What I wanted to achieve is to make mandatory attachment field on a catalog item to be true when Create a New Service is selected and false when not selected using script include and GlideAjax.

find_real_file.png

Here's my code using script include and GlideAjax.

Script Include

var getService6 = Class.create();
getService6.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	getMandatoryDetails: function() {
        var service6 = this.getParameter('sysparm_user');
        var gr = new GlideRecord('sc_cat_item');
        gr.addQuery('sys_id', service6);
        gr.query();
        // Query user records
        if (gr.next()) {
            gr.mandatory_attachment = true;
            gr.update();
        }
    },
    type: 'getService6'
});

Catalog Client Script

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

    var ga = new GlideAjax('getService6'); // Script Include Name
    ga.addParam('sysparm_name', 'getMandatoryDetails'); // Specific Function Name within the Script Include
    ga.addParam('sysparm_user', g_form.getValue('select_type_of_request')); // Value of the the field
    ga.getXML(setUpdate);

    function setUpdate(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        g_form.setValue('mandatory_attachment', answer);
    }

}

Any insights will be very helpful in understanding script include and GlideAjax. Thank you!

 

3 REPLIES 3

Anil Shewale
Mega Guru

hi Steven Karl

refer the following thread you can make field mandatory using onsubmit client script and script include refer the following thread it similar to your question.

https://community.servicenow.com/community?id=community_question&sys_id=60ebf7a4dbbdf3c82be0a851ca96...

https://community.servicenow.com/community?id=community_question&sys_id=832bc7eddb5cdbc01dcaf3231f96...

 

If it help mark helpful or correct.

Thanks and regards

Anil

TrevorK
Kilo Sage

This link may be of help to you - New Rocket has published their code for making attachments mandatory and it takes from the portal options. You may find it easier to either modify their code or examine it to see how they adjust the Portal Options to see if you can programmatically do it via your onChange script:

https://serviceportal.io/downloads/require-attachments/

It would likely be easier to have an information box or something displayed when the type of request requires an attachment and use an onSubmit to detect if there was one (and show an error / abort if needed). I know this one isn't a perfect fit (you want the mandatory to be dynamic) but I think it's at least a fall back if for some reason the above widget does not help you with the code you need.

Subhojit Das
Kilo Guru

Hi Steven,

Please refer the below link it may help you:

https://community.servicenow.com/community?id=community_question&sys_id=2c193f70db9dcc1423f4a345ca96...

Regards,

Subhojit Das