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 @Deepak Shaerma ,

 

Thanks for finding the spelling error do i need to write this inside the variable set or in catalog item the client script one?

 

Im trying but im getting this annotation 

New client-scripts are run in strict mode, with direct DOM access disabled. Access to jQuery, prototype and the window object are likewise disabled. To disable this on a per-script basis, configure this form and add the "Isolate script" field. To disable this feature for all new globally-scoped client-side scripts set the system property "glide.script.block.client.globals" to false.

 

Not sure what it means not faced it before and it says This catalog client script is not VA supported due to the presence of the following variables in the script: g_form.submitted

 

One more thing i observe is that it seems script include is not running or not sure i have added the log there but i don't see them in log table

Sarthak Kashyap
Mega Sage

Hi @suuriyas ,

 

I saw two issues in above given code, 1. syntax error - replace - sysparm_name, 2. GlideAjax is async so it will not wait for the response and it will submit the form. Instead you can try to use OnChange Client script.

 

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

Thanks and Regards,
Sarthak

HI @Sarthak Kashyap 

Thanks for replying

Client script i tried but did not work

One more thing i observe is that it seems script include is not running or not sure i have added the log there but i don't see them in log table

function onChange(control,oldValue, newValue){
//  if(newValue == oldValue){
//      return;
//  }
//  var fieldName = g_form.getFieldName(control);
//  if (fieldName == 'name'){
//      g_form.setValue('type', '');
//      g_form.setValue('target', '');
//      g_form.setValue('collection','');
//  }
// if (fieldName == 'type'){
//      g_form.setValue('target', '');
//      g_form.setValue('collection','');
//  }
// if (fieldName == 'target'){
//      g_form.setValue('collection','');
//  }


   var name = g_form.getValue('name');
   var type = g_form.getValue('type');
   var target = g_form.getValue('target');
   var table = g_form.getValue('collection');
   if(!name || !type || !target || !table){
    return;
   }
   var ga = new GlideAjax('SLAValidationUtils');
   ga.addParam('sysparm_name','checkDuplicate');
   ga.addParam('sysparm_name_val',name);
   ga.addParam('sysparm_type_val',type);
   ga.addParam('sysparm_target_val',target);
   ga.addParam('sysparm_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");

//    var response = ga.getXMLWait();
//    var result = response.documentElement.getAttribute("answer");
//    if (result == 'true'){
//  g_form.addErrorMessage("SLA with same name, type, target and table already exists");
//  return false;
   }

   });
}
 
spelling corrected in both client and server side

Hi @suuriyas ,

 

Please try to add logs/alert and check which line causing issue

 

SI - Must be glideAjax/Client callable

checkDuplicate: function() {

    var name = this.getParameter('sysparm_name_val');
    var type = this.getParameter('sysparm_type_val');
    var target = this.getParameter('sysparm_target_val');
    var table = this.getParameter('sysparm_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()){
        gs.log("Inside IF Condition") // User gs.info if working in scope app
        return "true";
    }else{
        return "false";    
     }

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

 

Client Script - OnChange

  var name = g_form.getValue('name');
   var type = g_form.getValue('type');
   var target = g_form.getValue('target');
   var table = g_form.getValue('collection');

alert("Name = " + name + "  Type = " + type + " Target = " + target + " Table = " + table )
   if(!name || !type || !target || !table){
    return;
   }
   var ga = new GlideAjax('SLAValidationUtils');
   ga.addParam('sysparm_name','checkDuplicate');
   ga.addParam('sysparm_name_val',name);
   ga.addParam('sysparm_type_val',type);
   ga.addParam('sysparm_target_val',target);
   ga.addParam('sysparm_table_val',table);

   ga.getXML(function(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
    alert("Answer = " + answer);
    if (answer == "true") {
         alert(" Inside if Answer True = " + answer);
        g_form.addErrorMessage("SLA with same name, type, target and table already exists");
        return;
     }
}

 

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

Thanks and Regards,

Sarthak

HI @Sarthak Kashyap ,

I have done some modification in the script and im almost there

Client script:

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue  ==='') {
      return;
   }
   var currentName = g_form.getValue('name');
   var currentType = g_form.getValue('type');

   if(!g_scratchpad.prevName)
   g_scratchpad.prevName = currentName;

   if(!g_scratchpad.prevType)
   g_scratchpad.prevType = currentType;

   if(currentName != g_scratchpad.prevName){
    g_scratchpad.prevName = currentName;
    g_form.clearValue('type');
    g_form.clearValue('target');
    return;

   }
   if(currentType != g_scratchpad.prevType){
    g_scratchpad.prevType = currentType;
    g_form.clearValue('target');
    return;
   }
   var name = currentName;
   var type = currentType;
   var target = g_form.getValue('target');
   var table = g_form.getDisplayValue('collection');

alert("Name = " + name + "  Type = " + type + " Target = " + target + " Table = " + table );
   if(!name || !type || !target || !table){
    return;
   }
   var ga = new GlideAjax('CheckContractSLA');
   ga.addParam('sysparm_name','checkDuplicate');
   ga.addParam('sysparm_name_val',name);
   ga.addParam('sysparm_type_val',type);
   ga.addParam('sysparm_target_val',target);
   ga.addParam('sysparm_table_val',table);

   ga.getXML(function(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
    alert("Answer = " + answer);
    if (answer == "true") {
         alert(" Inside if Answer True = " + answer);
        g_form.addErrorMessage("SLA with same name, type, target and table already exists");
        g_form.clearValue('collection');
       
        //return;
}});
}
 
script include
var CheckContractSLA = Class.create();
CheckContractSLA.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    checkDuplicate: function() {

    var name = this.getParameter('sysparm_name_val');
    var type = this.getParameter('sysparm_type_val');
    var target = this.getParameter('sysparm_target_val');
    var table = this.getParameter('sysparm_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()){
        gs.log("Inside IF Condition") // User gs.info if working in scope app
        return "true";
    }else{
        return "false";    
     }

    return gr.next() ? "true" : "false";
    }
});
 
this works for incident table alone but it is not working for other table.
im guessing in sla record the table field value show like Catalog Task [sc_task] but in my alert it show like catalog task because of that if fails i guess
 
can you please let me know how to correct it