using a client script to ensure 2 fields match on a form

patricklatella
Mega Sage

Hi all,

I'm creating a form for new hires, and on the form I want to have 2 fields where the user enters the new employee's ID #.   The intention of the 2nd field is to ensure that they enter the number correctly, much like when you have to repeat an email address or a password on banking stuff.

the 2 variables that need to match are:

emp_workday_id

emp_workday_id2

So the user first enters the value in the first field, and then needs to add the value again in the 2nd field (emp_workday_id2).   What need is, if the fields do match, then it triggers a GlideAjax to lookup the user record to populate some other fields on the form.   I have this part in place and working.  

If the 2 fields do not match, I need it to stop and give an alert that the 2 fields do not match.     Should I be putting this all in one catalog client script?   I'm thinking yes, but please advise.

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

if (newValue == ''){

g_form.setValue('first_name','');//name of field on form you want to auto populate

g_form.setValue('last_name','');//name of field on form you want to auto populate

g_form.setValue('supervisor','');//name of field on form you want to auto populate

g_form.setValue('emp_location','');//name of field on form you want to auto populate

}

var ga = new GlideAjax('u_employee_details_lookup_Ajax');//name of script include

ga.addParam('sysparm_name', 'getEmployeeDetails');//name of function on script include

ga.addParam('sysparm_user', g_form.getValue('emp_workday_id2'));//name of field on form triggering call

ga.getXML(EmployeeDetailsLookup); // Always try to use asynchronous (getXML) calls rather than synchronous (getXMLWait)

}

// Callback function to process the response returned from the server

function EmployeeDetailsLookup(response) {

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

var answers = answer.split(',');

g_form.setValue('first_name',answers[0]);

g_form.setValue('last_name',answers[1]);

g_form.setValue('supervisor',answers[2]);

g_form.setValue('emp_location',answers[3]);

}

now for the part that ensure the 2 fields I match I need help folding in something like this?

var ID1

var ID2

ID1=g_form.getValue(emp_workday_id);

ID2='current.emp_workday_id2';

//then something like

if   ID1=ID2//then it goes on to the GlideAjax line in my script above

else sMessage = 'Your entry does not match, please check the Workday ID number and re-enter.'; //and stop them from moving forward

am I headed in the right direction?

1 ACCEPTED SOLUTION

in this script showFieldMsg() syntax is wrong, please change it something like, and it should display the messge.



  g_form.showFieldMsg('emp_workday_id2' ,'Your entry does not match, please check the Workday ID # and re-enter');



Also, in earlier provided client script if condition has to be correct.



if (newValue == g_form.getValue('emp_workday_id')) field name is not in quotes



please check if this helps.




View solution in original post

29 REPLIES 29

Sorry Its My bad, Ignore my last reply. AS ID1 & ID2 totally new ID it doesn't exist in user table. Just implement one of the two script I provided above. Those will work


patricklatella
Mega Sage

thanks Arindam!   how does that fit into my script? i.e. where do I put that?


patricklatella
Mega Sage

this is not quite correct, but close?



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



if (newValue == ''){


g_form.setValue('first_name','');//name of field on form you want to auto populate


g_form.setValue('last_name','');//name of field on form you want to auto populate


g_form.setValue('supervisor','');//name of field on form you want to auto populate


g_form.setValue('emp_location','');//name of field on form you want to auto populate


}


{


if (newValue == g_form.getValue(emp_workday_id));{ //so if this is true I want it to move on to the GlideAjax


}


else alert('Your entry does not match, please check the Workday ID number and re-enter.');


g_form.clearValue('ID2', '');



var ga = new GlideAjax('u_employee_details_lookup_Ajax');//name of script include


ga.addParam('sysparm_name', 'getEmployeeDetails');//name of function on script include


ga.addParam('sysparm_user', g_form.getValue('emp_workday_id2'));//name of field on form triggering call


ga.getXML(EmployeeDetailsLookup); // Always try to use asynchronous (getXML) calls rather than synchronous (getXMLWait)


}




// Callback function to process the response returned from the server


function EmployeeDetailsLookup(response) {


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



var answers = answer.split(',');


g_form.setValue('first_name',answers[0]);


g_form.setValue('last_name',answers[1]);


g_form.setValue('supervisor',answers[2]);


g_form.setValue('emp_location',answers[3]);




}


patricklatella
Mega Sage

you are correct with the scenarios, see my added notes for what should happen in each:



Scenario-1: ID1 matches ID2 and ID1/ID2 exist in user table THEN IT MOVES ON TO POPULATE THE FIELD USING MY CLIENT SCRIPT, WHICH I'VE NOW MADE onChange BASED ON ID2...if the 2 match and there's a user record with a match, then it populates the fields.



Scenario-2: ID1 does not matches ID2 and ID1 exist in user table HERE IT SHOULD STOP THEM WITH THE ALERT TO CHECK THEIR ENTRIES



Scenario-3: ID1 matches ID2 and ID1/ID2 does not exist in user table. HERE I AM GOING TO WANT IT TO SOMEHOW AUTOMATICALLY CREATE A NEW USER RECORD.   AGAIN, THIS IS FOR A NEW HIRE, SO THEY HAVE A WORKDAY ID, BUT THEY MAY NOT YET HAVE A USER RECORD IN SERVICENOW, I WANT THE SYSTEM TO CREATE THE NEW USER RECORD IF THIS IS THE CASE.   This is the next thing I'll be looking to achieve.



Scenario-4: ID1 does not matches ID2 and ID1 does not exist in user table.HERE IT SHOULD STOP THEM WITH THE ALERT TO CHECK THEIR ENTRIES



So again, I think it's fine to just have the function of looking up the values to populate the fields on the form triggered by ID2, but I want it to only do that if the entry for ID2 matches ID1.



Does this help?


patricklatella
Mega Sage

can this be done with one catalog client script?