Issue in client callable Catalog client script

Kopal Garg
Tera Expert

Hello All,

 

I am new to GlideAjax and trying to check duplicate email address on a catalog form. I am getting "Failed to load resource: the server responded with a status of 404 (Not Found)" on submitting the form because of the script include and catalog script running in the background. Can anyone help what mistake I am making here?

-----------------------------Script include: "Name: EmailIDDuplicate" ---------------------------------

var EmailIDDuplicate = Class.create();
EmailIDDuplicate.prototype = Object.extendsObject(AbstractAjaxProcessor,{
duplicatecheck : function()
{
var email_ajax = this.getParameter('sysparm_a2');
var gr = new GlideRecord('sys_user');
gr.addQuery('email','email_ajax');
gr.query();
try
{
if(gr.next())
{
alert("Alert box of script");
return true;
}

else{
return false;
}
}
catch(err)
{
gs.log("Error is : "+err);
}
},
type: "EmailIDDuplicate"
});

 

---------------------------- Catalog client Script------------------------------

function onSubmit() {
//Type appropriate comment here, and begin script below

var email_value = g_form.getValue('email');

var ga = new GlideAjax('Email ID duplicate check');
ga.addParam('sysparm_a1', 'duplicatecheck');
ga.addParam('sysparm_a2', 'email_value');
ga.getXMLAnswer(checkemailResponse);

function checkemailResponse()
{
var answer = response.responseXML.documentElement.getAttribute("answer");
if(answer == 'true')
{
g_form.showErrorBox("Duplicate email");
g_form.setValue('email','');
}
else
{
return true;
}
}
}

 

Regards,

Kopal

1 ACCEPTED SOLUTION

Jim Coyne
Kilo Patron

Do you need the solution to support the Service Portal?  If not, then you can do it right in the onSubmit() Client Script:

function onSubmit() {
	var emailAddress = g_form.getValue("email");
	var gr = new GlideRecord("sys_user");
	gr.addQuery("email", emailAddress);
	gr.query();
	if (gr.next()) {
		g_form.showFieldMsg("email","Found an existing User record with the same email address", "error");
		return false;  //abort the submit
	} else {
		return true;  //carry on
	}
}

GlideAjax and onSubmit scripts do not really go together, as the result from the Script Include would normally arrive back at the client after the submission was completed and moved on from the form so the answer could not even be evaluated.

Because the check is happening on submission, the user is no longer interacting with the form, so there is no need to use an Ajax call.  They are just waiting on the submit to finish.

View solution in original post

12 REPLIES 12

Naveen4
Kilo Guru

Hi, it seems this is wrong here

ga.getXMLAnswer(checkemailResponse);

check this one.

 

Example code:

 

var ga = new GlideAjax('HelloWorld'); //Here HelloWorld is the script include name

ga.addParam('sysparm_name','helloWorld'); // we are calling helloWorld function

ga.addParam('sysparm_user_name',"Bob"); //we are passing paramter Bob

ga.getXML(HelloWorldParse); or // ga.getXMLWait();

 function HelloWorldParse(response) {

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

     alert(answer);

}

 

Prateek Gupta3
Mega Guru

Hi,

 

Replace "sysparm_a1" with "sysparm_name".

 

Thanks,

Prateek

ScienceSoft
Tera Guru

Hi Kopal Garg,

To pass variable value as query parameter, don't need to use quotes:
Your code

gr.addQuery('email','email_ajax');

Should be replaced with

gr.addQuery('email',email_ajax);

To call Script using AJAX, you need:

1. Create GlideAjax object:

var ga = new GlideAjax('your_script_name');

2. Specify options for calling a function from ершы script:

ga.addParam('sysparm_name', 'function_name'); // function name to call
// [Optional] if function has parameters
ga.addParam('sysparm_parameter_name', "Bob");

3. Use callback function to call the script function:

ga.getXML(someFunction);

function someFunction(response) {
  var answer = response.responseXML.documentElement.getAttribute("answer");
  // place your code here
}

 

Somveer Karhan1
Giga Expert
Hello Kopal, 1. Make sure the Client Callable check box is selected in the script include. 2. In client script: - Call the script include by exact name and specify the function with syaparm_name parameter as below. var ga = new GlideAjax('EmailIDDuplicate'); ga.addParam('sysparm_name', 'duplicatecheck'); - Execute the server side code using getXML() ga.getXML(checkemailResponse); Mark correct, helpful based on the impact.