validate entered email is exists in ServiceNow or not?

Dinesh
Tera Guru

I have created email field on service portal and user manually entering an email so which need to check whether the provided email id is exists in snow or not? if  it is exists we need to clear the value. I have written client script and script include not working.

 

 

find attachment to view my code.

 

 

 

1 ACCEPTED SOLUTION

Dinesh
Tera Guru

Finally working  as per the below  script:

 

Scriptinclude:

 

 

var EmailValidation = Class.create();
EmailValidation.prototype = Object.extendsObject(AbstractAjaxProcessor, {

checkExistingUser: function() {
var userExist = "false";
var emailaddress = this.getParameter('sysparm_email');
var gr = new GlideRecord("sys_user");
gr.addQuery("email", emailaddress);
gr.query();
if (gr.next()) {
userExist = "true";
}
return userExist;
},
type: 'EmailValidation'
});

-------------------------------or--------------------------------------
var EmailValidation = Class.create();
EmailValidation.prototype = Object.extendsObject(AbstractAjaxProcessor, {

checkExistingUser: function() {
var emailaddress = this.getParameter('sysparm_email');
var gr = new GlideRecord("sys_user"); 
gr.addQuery("email", emailaddress);
gr.query();
if (gr.next()) {
return true;
} else {
return false;
}
},
type: 'EmailValidation'
});

 

 

 

client script:

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

var ga = new GlideAjax('EmailValidation');
ga.addParam('sysparm_name', 'checkExistingUser');
ga.addParam('sysparm_email', newValue);

ga.getXML(gaResponse);

function gaResponse(response) {

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

alert(answer);
if (answer == "true") {

g_form.clearValue('email_id');


}


}

}

View solution in original post

8 REPLIES 8

saran4
Kilo Guru

In your script please correct the spelling(your spelling sysparam) sysparm this this the correct spelling, also change your client script UI type to All

 

please mark helpful and correct if my answer satisfies your query 

still not working:

 

Scriptinclude:

 

var EmailValidation = Class.create();
EmailValidation.prototype = Object.extendsObject(AbstractAjaxProcessor, {

checkExistingUser: function() {
var userExist = "false";
var emailaddress = this.getParameter('sysparm_email');
var gr = new GlideRecord("sys_suer");
gr.addQuery("email", emailaddress);
gr.query();
if (gr.next()) {
userExist = "true";
}
return userExist;
},
type: 'EmailValidation'
});

 

client script:

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

var ga = new GlideAjax('EmailValidation');
ga.addParam('sysparm_name', 'checkExistingUser');
ga.addParam('sysparm_email', newValue);

ga.getXML(gaResponse);

function gaResponse(response) {

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


if (answer == "true") {

g_form.clearValue('email_id');


}


}

}

 

 

Hi @Dinesh ,

Use below script include :-

 

var EmailValidation = Class.create();
EmailValidation.prototype = Object.extendsObject(AbstractAjaxProcessor, {

checkExistingUser: function() {
var emailaddress = this.getParameter('sysparm_email');
var gr = new GlideRecord("sys_user"); //made mistake in user table name. now corrected.
gr.addQuery("email", emailaddress);
gr.query();
if (gr.next()) {
return  true;
}else{
return  false;
}
},
type: 'EmailValidation'
});

 


Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy

Still not working, email field value is not clearing..