Glideajax method is not working

suuriyas
Tera Contributor

HI Community,

 

I have a requirement, in a catalog item we have variables like name, type, target and table. when user fill the details and while submit the form , in sla definition table if we have records with same name, type , table and target then it should not submit the form it should throw an error.

for this i have tried client script and script include but form is getting submitted any how  not sure what is wrong in it.

client script;

function onSubmit() {
    if(g_form.submitted){
        return true;
    }
    var mrvsData = g_form.getValue('sla_configuration');
    if(!mrvsData) return true;
   
    var rows = JSON.parse(mrvsData);
    if(!rows || rows.length === 0){
        return true;
    }
    var name = rows[0].name;
    var type = rows[0].type;
    var target = rows[0].target;
    var table = rows[0].collection;

    var ga = new GlideAjax('SLAValidationUtils');
    ga.addParam('sysparam_name', 'checkDuplicate');
    ga.addParam('sysparam_name_val', name);
    ga.addParam('sysparam_type_val', type);
    ga.addParam('sysparam_target_val', target);
    ga.addParam('sysparam_table_val', table);

   ga.getXML(function(response){
    var answer = response.responseXML.documentElement.getAttribute("answer");

    if (answer == 'true') {
        g_form.addErrorMessage("SLA with same name, type, target and table already exists");
        return false;
    }
g_form.submitted = true;
g_form.submit();
   });
    return false;
    // alert ('onsubmit is running');
    // return false;
}
script include;
var SLAValidationUtils = Class.create();
SLAValidationUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkDuplicate: function(){
    var name = this.getParameter('sysparam_name_val');
    var type = this.getParameter('sysparam_type_val');
    var target = this.getParameter('sysparam_target_val');
    var table = this.getParameter('sysparam_table_val');

    var gr = new GlideRecord('contract_sla');
    gr.addQuery('name',name);
    gr.addQuery('type',type);
    gr.addQuery('target',target);
    gr.addQuery('collection',table);
    gr.query();

    if(gr.next()){
        return 'true';
    }
return 'false';
}
    //type: 'SLAValidationUtils'
});

 

13 REPLIES 13

Hi @suuriyas ,

 

Try add change your client script line with below line 

Your line -

var table = g_form.getDisplayValue('collection');

Change with - 

var table = g_form.getValue('collection');

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards,

Sarthak

HI @Sarthak Kashyap ,

 

Yeah i tried that first if i add that 

var table = g_form.getValue('collection');
then it shows sysid and it wont matching so return false because of that i used displayvalue but it works only for incident and not for other tables which have different backend name like catalog task

Hi @suuriyas ,

 

Maybe you can send the sys_id of your table/collection and get it in your script include and in your script include you can add one glideRecord on sys_db_object table to fetch table name and add that table name in your Glide record.

 

For example 

Client script

var table = g_form.getValue('collection');
 
Script Include
 
Do one more glide record on sys_db_object, 
 
var table = this.getParameter('sysparm_table_val'); // you are sending the table sys_id from CS
 
var tableName = "";
var gr = new GlideRecord("sys_db_object");
if(gr.get(table)){
     tableName = gr.getValue("name");
}
 
Now use tableName in your contract_sla Glide Record
   var gr = new GlideRecord('contract_sla');
    gr.addQuery('name', name);
    gr.addQuery('type', type);
    gr.addQuery('target', target);
    gr.addQuery('collection',tableName);
 
 
This is just code snippet, modify as per your requirment
 
 

Please mark my answer correct and helpful if this works for you

Thanks and Regards,

Sarthak

ChallaR
Giga Guru

Hi @suuriyas ,

 

i have updated few link in the script can you please check below 

Client script -

function onSubmit() {
    var mrvsData = g_form.getValue('sla_configuration');
    if (!mrvsData) return true;

    var rows = JSON.parse(mrvsData);
    if (!rows || rows.length === 0) return true;

    var name = rows[0].name;
    var type = rows[0].type;
    var target = rows[0].target;
    var table = rows[0].collection;

    var ga = new GlideAjax('SLAValidationUtils');
    ga.addParam('sysparam_name', 'checkDuplicate');
    ga.addParam('sysparam_name_val', name);
    ga.addParam('sysparam_type_val', type);
    ga.addParam('sysparam_target_val', target);
    ga.addParam('sysparam_table_val', table);

    var response = ga.getXMLWait(); // synchronous call
    var answer = response.documentElement.getAttribute("answer");

    if (answer == 'true') {
        g_form.addErrorMessage("SLA with same name, type, target and table already exists");
        return false; // stop submission
    }
    return true; // allow submission
}

 

 Script include -

var SLAValidationUtils = Class.create();
SLAValidationUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    checkDuplicate: function() {
        var name = this.getParameter('sysparam_name_val');
        var type = this.getParameter('sysparam_type_val');
        var target = this.getParameter('sysparam_target_val');
        var table = this.getParameter('sysparam_table_val');

        var gr = new GlideRecord('contract_sla');
        gr.addQuery('name', name);
        gr.addQuery('type', type);
        gr.addQuery('target', target);
        gr.addQuery('collection', table);
        gr.query();

        return gr.next() ? 'true' : 'false';
    }
});

NOTE -

 

  • Use getXMLWait() for synchronous validation in onSubmit.
  • Do not call g_form.submit() manually inside the callback.
  • Always return false when validation fails.

Thanks,

Rithika.ch