onSubmit Catalog Client script not working in Service portal

Bargavi
Mega Expert

Hi,

There is a field "App Code" in one of the catalog items which has a validation to check if the user is entering already existing app code, in case he is entering the existing one it should alert   "Requested app code already exists choose a different one".

I am using the below script:

var ui_app_code = g_form.getValue('u_requested_app_code');
var gr = new GlideRecord('cmdb_ci_appl');
gr.addQuery('u_application_code', ui_app_code);      
gr.query();

while(gr.next())
{

alert (" Requested App code already exists. Please choose a different one...");
return false;
}
}

If I am trying to submit the catalog item using the existing app code from an ESS page I have an alert and the form does not get submitted but the same is not working in service portal. can some one help me on what modifications has to be done to the code to make it work in service portal.

Thanks,

Bargavi

1 ACCEPTED SOLUTION

Bargavi
Mega Expert

was able to resolve it using thread Catalog Client Script asyncrhonous query to stop submission onSubmit.



Thanks for replying



-Bargavi


View solution in original post

12 REPLIES 12

Could you provide the solution you put in place? Thanks!


Yes sure..



Below   is the script:



function onSubmit() {  
//If ServicePortal  
if (!window) {  
if (g_scratchpad.isFormValid) {  
              return true;  
  }  
      g_form.hideAllFieldMsgs('error');  
  var actionName = g_form.getActionName();  
 
  //Application code contain letters numbers and dashes only  
  var app_code = g_form.getValue('field_name');  
 
  //Check if entered Code already exists  
  var ac = new GlideRecord(cmdb_ci_application');  
  ac.addQuery('u_application_code', app_code);  



  //Callback function to control stop submit asynchronously  
  ac.query(function() {  
  while (ac.next()) {  
    alert('Requested Application Code already exists, Please choose a different one..!' );  
  return false;  
  }  
              g_scratchpad.isFormValid = true;  
              g_form.submit(actionName);  
  });  

  return false;  
}  

}
/* if CMS
function onSubmit()
{  
//var ui_app_code = g_form.getValue('u_requested_app_code');
//alert ('Requested App Code:, ui_app_code' );
var gr = new GlideRecord('cmdb_ci_appl');
gr.addQuery('u_application_code', g_form.getValue('u_requested_app_code'));    
gr.query();
if (gr.hasNext())
{
alert("Requested App code already exists. Please choose a different one...");
return false;
}
}
*/




Please let me know whether the solution was helpful or not.



-Bargavi


Hello Bhargavi,




Can I know what exactly these two lines are doing.


  1. g_scratchpad.isFormValid = true;  
  2. g_form.submit(actionName);

Because i have used same code with out using g_scratchpad.isFormValid and g_form.submit(actionName), how these two lines are stoping the asynchronous function.I got confused can you brief me..?



Thanks,


Nithin.