Restrict form submission when the script include return a value to onsubmit client script.

Sangeetha7996
Tera Contributor
If the user selects a particular Business service (reference field) in the incident record producer, it should restrict the form submission. For others it should allow.
Business service field refers to the cmdb table. In the cmdb table, we have an attribute that holds the sys id of its dedicated record producer/ catalog form.
We have used onsubmit client script and script include to retrieve the sys id of the record producer from the cmdb table. If it retrieve sys id then form submission has to disabled.
 
Script include: client callable is enabled
var recordProducerNavigationUrl = Class.create();
recordProducerNavigationUrl.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getrecprodid: function() {
        var buscId = this.getParameter('sysparm_busc');
        var recordProdId = new GlideRecord('cmdb_ci_service');
        recordProdId.addQuery('sys_id', buscId);
        recordProdId.query();
        if (recordProdId.next()) {
            var result = recordProdId.u_incident_record_producer.toString();
            return JSON.stringify(result);

        } else {
            var resultE = '';
            return JSON.stringify(resultE);
        }
    },
    type: 'recordProducerNavigationUrl'
});
 
Client Script: 
function onSubmit() {
 
    if (!g_form.ajaxComplete) {
        ajaxCall();
        return false;
    }
}
 
function ajaxCall() {
 
    var busc = g_form.getValue('business_service');
    var instance = new GlideAjax('recordProducerNavigationUrl');
    instance.addParam('sysparm_name', 'getrecprodid');
    instance.addParam('sysparm_busc', busc);
    instance.getXMLAnswer(function(response) {
g_form.addInfoMessage(response);
        if (response) {
            g_form.ajaxComplete = true;
            g_form.submit();
 
        }
 
    });
5 REPLIES 5

Priyanka_786
Tera Guru
Tera Guru

@Sangeetha7996 : What is issue/question here you are looking for?

Catalog form should not allow user to submit when certain business service is selected. (should be dynamic)

@Sangeetha7996 Okay.

 

This will not work as per ask because you're using an asynchronous call to the server and the client script isn't waiting for a response to come back.  While this is best practice, it simply won't work in an 'onSubmit' script for validation because the submit happens and redirects without waiting.  You need to use a synchronous call whenever you need this type of response. In this case,you need to use onChange Client script on field of Business Service by calling the script include.

 

Hope it helps.

Please mark my response as accepted/helpful if it helps.

Regards,

Priyanka Salunke

How can I restrict form submission with an onChange script.