How to check whether first name and last name entered on the form already exist or not ?

Venkatesh43
Tera Contributor

Hi experts,

I have two fields on the form, first name and last name and I have a back end table in which all user attributes are present including first name and last name.Once the user enters first name and last name I would like check whether combination of first name and last name already present in table or not, if present give an alert message. Find the below image

find_real_file.png

if a user with same first name and last already exist then have to give a pop-up message in the form.I wrote two scripts, onchange of first name and onchange of last name but it is leading to an infinity loop as both are working one after another. Is there any better idea to fulfill this requirement ??

Regards,

Venkatesh

5 REPLIES 5

Mike Patel
Tera Sage

Instead of onChange, You can create onSubmit client script that validates both fields together.

Moy1
Kilo Guru

Although I would not recommend it since this would make the form slower and affect user experience, you can write an onChange client script like so:

 

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

   var firstName = g_form.getValue('first_name');
    var lastName = g_form.getValue('last_name');
    
    var a = new GlideRecord('sys_user'); //replace with your back end table name//
    a.addQuery('first_name',firstName);
    a.query();
    a.addQuery('last_name',lastName);
    a.query();
    
    if(a.next()){
    alert('user exists');    
    }
    else{
        alert('no problemo');
    }
   
}

 

Apply this client script on change of the last name field. Do not apply any script on the first name field.

Venkatesh43
Tera Contributor

Hi Moy,

Thanks for your response. If I write script only on change of last name, It will work only if user enters first name then last name but it doesn't if user first enters last name then first name. I would like to validate in both the ways. Can I do it with business rule ?

regards,

venkatesh

Sure, you can do it through a business rule.

 

find_real_file.png

 

find_real_file.png

 

    var a = new GlideRecord('sys_user'); //replace with your back end table name//
    a.addQuery('first_name',current.first_name);
    a.query();
    a.addQuery('last_name',current.last_name);
    a.query();
    
    if(a.next()){    //check if user present already
    current.setAbortAction(true);    
    gs.addErrorMessage('user exists, please try again');    
    }
    else{
        gs.addInfoMessage('User added');
    }