Restrict submit if there are no attachments attached on the form depending on certain conditions

satya30
Tera Contributor

In the form: If the url value is empty it should check if there are any attachments attached. if attachment is there it should submit otherwise it should not submit instead throw an error message.

In the below script that I am writing:

g_form.getUniqueValue() is not giving the record value that is going to be created.( It is giving me sys_id of constant record all the time).

Client Script:

function onSubmit() {
    var url = g_form.getValue('file_upload_url');
    g_form.addInfoMessage(url);

    if (!url) {
        g_form.addInfoMessage("alert2:");

        var sysid = g_form.getUniqueValue();
        alert("Check id = " + sysid);
        var gr = new GlideAjax("sn_customerservice.getAttachmentCount");
		gr.addParam('sysparm_name', 'getCount');
        gr.addParam("sys_attach", sysid);
        gr.getXMLAnswer(validate);

    } 
    function validate(answer) {
        g_form.addInfoMessage("answer" + answer);

        if (answer == false || answer == 'false') {
            alert('Please attach Photo(s) or add URL to proceed with the submission of your request');
            return false;
        } else {
            return true;
        }
    }
}

Script Include:

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


    getCount: function() {
        var valid = false;
        var attachId = this.getParameter("sys_attach");
		gs.info("Harika"+attachId);
        var gr = new GlideRecord("sys_attachment");
        gr.addQuery('table_sys_id', attachId);
		gr.query();
		if(gr.getRowCount()>1){
        if (gr.next()) {
			gs.info("Harika"+gr.table_sys_id);
            valid = true;
        }
		}
        return valid;
    },
    type: 'getAttachmentCount'
});


Please help me on how should I achieve this requirement.

15 REPLIES 15

@satya30 

the latest script I shared is not working?

It worked for me in the OOTB sc item widget which is normally used to render the catalog item or record producer

I believe I have answered your question, you never mentioned in your original question about custom portal and custom widget

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Yeah, I just figured out that the issue is because of the custom portal where we are using widget

@satya30 

Glad to know.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

@satya30 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

@satya30 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader