Catalog Client Script - check for duplicate email address

kchorny
Tera Guru

We have a catalog item that is used to create user records.   There is currently no validation to prevent the user from continuing the process if the user already exists.   So I'm trying to validate that the email address being entered isn't a duplicate.

This script is not working...   please tell me what I need to do to make it work.   I would also like to abort the submission with an alert and direct the user back to the Catalog Item or Service Catalog, rather than just showing an error message.

cscript.GIF

I also tried, in place of lines 4 and 5:

var eAdd = newValue.trim();

which didn't work either.  

Thanks for any help!

1 ACCEPTED SOLUTION

Ujjawal Vishnoi
Mega Sage
Mega Sage

Hi Karla,



If you want to achieve it by onchange client script then you can use below mentioned script.



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


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


  return;


  }


  var usr = new GlideRecord('sys_user');


  usr.addQuery('email',newValue);


  usr.query();


  if(usr.next())


  {


  alert('Email Address already exixts');


  g_form.setValue('email','');


  }


}



and If you want to achieve it by onsubmit you can use below code.



function onSubmit() {


  var usr = new GlideRecord('sys_user');


  usr.addQuery('email',g_form.getValue('email'));


  usr.query();


  if(usr.next())


  {


  alert('Email Address already exixts');


  g_form.setValue('email','');


  return false;


  }


}



Both will work.



Hope this helps.



Regards


Ujjawal


View solution in original post

6 REPLIES 6

In the above screenshot showing, you return code when form is loading or newValue is empty. So you need to close the if loop of isLoading.


You need to change the code like this,


Please change your code to,


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


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


  return; // previously you have return the code entire here


  }


  var gr = new GlideRecord('sys_user');


  gr.addQuery('email',newValue);


  gr.query();


  if(gr.hasNext())


  {


  g_form.showFieldMsg('app_email','User already exists, stop processing this request','error');


  }


}


For better thing go with onChange Ajxa call.


http://wiki.servicenow.com/index.php?title=GlideAjax#gsc.tab=0


kchorny
Tera Guru

I did some more research and found the top.window.location statement, that gets me what I need.   The final script, applied to a variable set, is:



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


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


    return;


  }


  //alert('Client script is running');


    var usr = new GlideRecord('sys_user');


    usr.addQuery('email',newValue);


    usr.query();


    if(usr.next())


    {


    var URL = 'https://myinstance.service-now.com/navpage.do';


    alert("A user with this email address already exists. You will be returned to the main page, where you may submit a ticket to get the user's permissions changed.");


    top.window.location = URL;


   


    }


}