onSubmit Catalog Client Script and GlideAjax is Not Working on Service Portal

SNOW User8
Giga Guru

Hi All,

I am having an onSubmit Catalog Client Script on Service portal to prevent it from Submitting without Subcategory2 value, But it is not working.

And also I have tried the same using GlideAjax, that is also failing...

Please find my code below,

Title: Catalog Client Script

function onSubmit() {

function onSubmit() {

    //Type appropriate comment here, and begin script below

  var form = typeof g_sc_form != "undefined" ? g_sc_form : g_form;

  var subCat1Value = g_form.getValue('u_subcategory');

  var subCat2Value = form.getValue('u_subcategory_2');

  var gp = new GlideRecord('sys_choice');      

  gp.addQuery('name', 'change_request');      

  gp.addQuery('dependent_value', subCat1Value);      

  gp.addQuery('element', 'u_subcategory_2');      

  gp.addQuery('inactive', 'false');      

  gp.query(function(gp) {  

  if(!gp.hasNext()){

        return true;

  }

      if(gp.hasNext() && subCat2Value == 'none'){

  alert('Please select atleast one value from Subcagtegory 2');

  return false;       // The code automatically get submitted without a Subcategory2 value after getting above mentioned alert

  }

  } );

}

  }

  } );

}

Can anyone help me..

Thanks In advance.

1 ACCEPTED SOLUTION

SNOW User8
Giga Guru

The following code has worked for me...



function onSubmit() {


  //Type appropriate comment here, and begin script below


  if (!window) {


  if (g_scratchpad.isFormValid) {


  return true;


  }


  var form = typeof g_sc_form != "undefined" ? g_sc_form : g_form;


  var subCat1Value = g_form.getValue('u_subcategory');


  var subCat2Value = form.getValue('u_subcategory_2');



  var gp = new GlideRecord('sys_choice');


  gp.addQuery('name', 'change_request');


  gp.addQuery('dependent_value', subCat1Value);


  gp.addQuery('element', 'u_subcategory_2');


  gp.addQuery('inactive', 'false');


  gp.query(function(gp) {


  if(gp.getRowCount() >= 1 && subCat2Value == 'none'){


  alert('Please select atleast one value from Subcagtegory 2');


  return false;


  }


  g_scratchpad.isFormValid = true;


  g_form.submit();


  });


  return false;



}


}


View solution in original post

9 REPLIES 9

reginabautista
Kilo Sage

Could it be this line? you're missing 'g_'



var subCat2Value = g_form.getValue('u_subcategory_2');


SNOW User8
Giga Guru

Hi All,



Thank you all for your response.



I am able to get the values and control is going inside and getting the alert   "Please select at lease one value from Subcagtegory 2".



But my problem is after getting the alert the record getting submitted automatically with out Subcategory2 chosen.


Midhun1
Giga Guru

Hi Silva,



Have you tried Synchronous Glide Ajax?


SNOW User8
Giga Guru

Hi Midhun Golla,



Thanks for your response,



Yeah I have tried both Synchronous and Asynchronous Glide Ajax.


SNOW User8
Giga Guru

The following code has worked for me...



function onSubmit() {


  //Type appropriate comment here, and begin script below


  if (!window) {


  if (g_scratchpad.isFormValid) {


  return true;


  }


  var form = typeof g_sc_form != "undefined" ? g_sc_form : g_form;


  var subCat1Value = g_form.getValue('u_subcategory');


  var subCat2Value = form.getValue('u_subcategory_2');



  var gp = new GlideRecord('sys_choice');


  gp.addQuery('name', 'change_request');


  gp.addQuery('dependent_value', subCat1Value);


  gp.addQuery('element', 'u_subcategory_2');


  gp.addQuery('inactive', 'false');


  gp.query(function(gp) {


  if(gp.getRowCount() >= 1 && subCat2Value == 'none'){


  alert('Please select atleast one value from Subcagtegory 2');


  return false;


  }


  g_scratchpad.isFormValid = true;


  g_form.submit();


  });


  return false;



}


}