Unable to submit record on catalog client script

Anish9515
Tera Contributor

If (answer == true )record should be submit , But unable to submit the record

 

Catalog client script on submit :-

var isAlertDisplayed = false; // Flag to prevent multiple alerts

function onSubmit() {
var getvalue = g_form.getValue('spreadsheet_name');

if (g_form.getValue('fnd_actions') = 'fnd_action_create') {
var name = new GlideAjax('BIvalidationresultingNameAJAX');
name.addParam('sysparm_name', 'validationname');
name.addParam('sysparm_ci_name', g_form.getValue('spreadsheet_name'));
name.getXML(Callback);

// Return false here to prevent immediate form submission
return false;
} else {
// If fndActions is 'fnd_action_modify', allow form submission
return true;
}
}

function Callback(response) {
if (isAlertDisplayed) {
return false; // Avoid executing multiple times
}

var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);

if (answer === 'false') {
isAlertDisplayed = true; // Set the flag
alert("Resulting Name Interface already exists. Please provide a new Resulting Name Interface.");
g_form.addErrorMessage("Resulting Name Interface already exists. Please provide a new Resulting Name Interface.");
return false; // Prevent form submission
} else {
return true; // Allow form submission
}
}
Script include :-

var BIvalidationresultingNameAJAX = Class.create();
BIvalidationresultingNameAJAX.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    validationname: function() {
        var ci = this.getParameter('sysparm_ci_name');
        gs.log("Parameter sysparm_ci_name: " + ci);
        
        var gr = new GlideRecord('cmdb_rel_ci');
        gr.addQuery('parent.name', ci);
        gr.query();
 
        
        if(gr.next()) {
 
            return false; 
        }
        else{
 
        return true; 
 
}
},
 
    type: 'BIvalidationresultingNameAJAX'
});

@Ankur Bawiskar 

Can anyone help me 

1 REPLY 1

Arpan Baishya
Kilo Sage

Hi @Anish9515,

 

Could you please try rewriting your code in a different way?

 

Something along the lines of 

function onSubmit() {
    var ci_name = g_form.getValue('spreadsheet_name');

    if (g_form.getValue('fnd_actions') = 'fnd_action_create') {

        if (validation_function(ci_name)) {
            return true;
        }

        else {
            g_form.addErrorMessage("Resulting Name Interface already exists. Please provide a new Resulting Name Interface.");
            return false;
        }

    }
}

function validation_function(ci_name) {
    // Perform validation using GlideAjax
    // return true or false based on the business logic

}

Let me know if this helps.

Also, you may want to check out this link. - Asynchronous onSubmit Catalog/Client Scripts in ServiceNow - ServiceNow Developer Pro-Tips (snprotip...