Validate Email Address in service catalog form

phil34
Tera Expert

Hi All,

 

I am sure someone has already done this, but I can't find a solution.

 

I have a catalogue item that is used to request an email address. The variable is "u_email_address"

When a user inputs an email address in that field I want to check if the email address already exists in the user table in our service now instance. This is what I want to happen when they are completing the form.

 

User types requested address in the field.

phil34_0-1689033535639.png

If the address entered by the user is already in the user table, then I want a message box to appear saying.

 

"The requested address is already in use"

 

When the user clicks on the OK button of the message box the text is cleared from the field and the user is required to enter another email address.

 

If they enter an email address that is NOT already in the user table then they can move to the next field.

 

I am new at scripting so I might need a bit of hand-holding if it is more complex. Hope you can help.

Cheers

Phil

 

phil34_2-1689034291794.png

 

 

1 ACCEPTED SOLUTION

Hey Phil,

 

You did almost right,

 

Use the below code in client script,

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
   }
    var usrGA = new GlideAjax('Validate_Email');
    usrGA.addParam('sysparm_name''validateEmail');
    usrGA.addParam('sysparm_email', newValue);
    usrGA.getXMLAnswer(_result);

    function _result(answer) {
        if (answer=='true') {
            alert('User Name already exists');
            g_form.clearValue('u_email_address');
        }
    }
}
Hope it helps and please mark helpful if it solves the issue.
Thanks,
Kiran
 

 

View solution in original post

6 REPLIES 6

Kiran_45
Giga Guru

Hi @phil34 ,

This is an easy one for you.

1) Make use of onchange catalog client script, use glide ajax pass the value of email adress to the  Script include.

2) In script include glide to user table compare the email address. If true return false or vice versa.

Hope it helps!.

Hi Kiran,

 

Thanks for answering, I have gone down that track and below is what I have configured but I cannot get it to behave, Regardless of what email address I put into the field the alert\message box always appears.

 

Maybe you can spot my mistake?

 

phil34_0-1689046677180.png

SCRIPT:

var Validate_Email = Class.create();
Validate_Email.prototype = Object.extendsObject(AbstractAjaxProcessor, {
validateEmail: function() {

var currentEmail = this.getParameter('sysparm_email');
var userGR = new GlideRecord('sys_user');
userGR.addQuery('email', currentEmail);
userGR.query();
if (userGR.next()) {

return true;
} else {
return false;
}


},

type: 'Validate_Email'
});

 

phil34_1-1689046747535.png

 

SCRIPT:

function onChange(control, oldValue, newValue, isLoading) {

if (isLoading || newValue == '') {
return;

}
var usrGA = new GlideAjax('Validate_Email');
usrGA.addParam('sysparm_name', 'validateEmail');
usrGA.addParam('sysparm_email', newValue);
usrGA.getXMLAnswer(_result);

function _result(answer) {

if (answer) {
alert('User Name already exists');
g_form.clearValue('u_email_address');
}

}


}

Hey Phil,

 

You did almost right,

 

Use the below code in client script,

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
   }
    var usrGA = new GlideAjax('Validate_Email');
    usrGA.addParam('sysparm_name''validateEmail');
    usrGA.addParam('sysparm_email', newValue);
    usrGA.getXMLAnswer(_result);

    function _result(answer) {
        if (answer=='true') {
            alert('User Name already exists');
            g_form.clearValue('u_email_address');
        }
    }
}
Hope it helps and please mark helpful if it solves the issue.
Thanks,
Kiran
 

 

Hi @phil34 

 

The answer which is returned from Server side is not "Boolean", but a string. Please modify your client script function accordingly. 

************************************************************************

 alert('the answer is' + typeof(answer));
    if (answer=='true') {
        alert('User Name already exists');
        g_form.clearValue('u_email_address');
    }

 ******************************************************