- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2022 02:50 AM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2022 03:58 AM
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');
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2022 03:45 AM
Hi @Dinesh ,
I have put alert on client script. use below client script and check what you are getting in alert message.
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='+answer); //alert to print response
if (answer == "true") {
g_form.clearValue('email_id');
}
}
}
Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2022 03:33 AM
Hi, consider to perform a validation of an email field via onSubmit Catalog Client Script.
More details - in Support article,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2022 03:58 AM
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');
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2022 04:02 AM
Hi @Dinesh ,
Glad to know that it is working.
Please mark appropriate response as correct and close the thread.