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

You need to create two client Script. Otherwise it would be complex.



Step 1:   Client script for onchange for ID1 field. That which will check if ID1 matches in user table.



Step 2: Client script for onchange for ID2 field. You already created that and ihere is the updated script.  



Script include:



var u_New_Hire_Scripts_Ajax = Class.create();



u_New_Hire_Scripts_Ajax.prototype = Object.extendsObject(AbstractAjaxProcessor, {



getEmployeeDetails: function(){


var retVal = ''; // Return value


var user = this.getParameter('sysparm_user');



var detailsRec   = new GlideRecord('sys_user');//table where desired variable lives


detailsRec.addQuery('user_name',user);


detailsRec.query();



// Query user records


if(detailsRec.next()) //if there is match, it does the retVal.   is this where I need to return something saying there is no match and to trigger the "emp_workday_id2" field?


{



retVal = detailsRec.first_name+','+detailsRec.last_name+','+detailsRec.manager+','+detailsRec.location;


}



return retVal;




},


});


Client script:


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(g_form.getValue(emp_workday_id) == ''){


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


} // this if condition added for the scenario when user doesn't fill ID1 and fill ID2 which matches the user record.



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


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)


}


else {


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


g_form.clearValue('ID2', ''); //as is, the system is not liking this here


}


}



// 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]);



if (answer == ''){


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


g_form.clearValue('ID2', ''); //as is, the system is not liking this here


}


}



Hope this should work.


Brian Dailey1
Kilo Sage

Hi Patrick,



We perform a similar validation on employee IDs/etc., but in a slightly different way.   To paraphrase my approach for your scenario, I would have only one ID field (e.g., "emp_workday_id") and use an onChange script for that field to prompt the user to verify their entry before proceeding any further.




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


        if(isLoading || oldValue == newValue){


                  return;


        }


        var verification = '';


        while(verification != newValue){


                verification = prompt("Once the record is saved, this cannot be changed.", "Please Confirm (retype) Entry");


                if(verification != newValue){


                        if(!confirm("Confirmation did not match, try again?")){


                                g_form.setValue('emp_workday_id', oldValue);


                                return;


                        }


                }


        }


        // Following the verification, you can then insert your GlideAJAX code here...


}




You could give that a try.




Thanks,


-Brian


patricklatella
Mega Sage

Hi Brian,


thanks so much...this could very well be the solution.   And actually a couple things about what I'm trying to do have changed, but a little more context...about my requirement, so the entry in the first "emp_workday_id" field is sending a query to the sys_user table to find the record that matches.   Then it is returning the values to populate the other fields (seen in the screen shot earlier in the thread).   When the match is found and the other fields populate, the 2nd Workday ID field goes away because the user can validate right then that they have the right record by looking at the name fields.   All good there.



However, if there is no match on the sys_user table for the first workday ID number to find, then the (because this is all part of an order guide for new hires) 2nd workday ID number field is meant to ensure the user enters this value correctly because when they finish with this page in the Order page and move to "Change Options" I'm going to have an onSubmit client script that creates a new record in the sys_user table for this new hire.



So really where I'm at is just trying to figure out how to ensure that the entries in these 2 fields match...(which it will only be possible to enter a value in both of these if there's no match in the sys_user table for the value in the 1st Workday ID field)...and to have a message and a block at that point.



So I'm tinkering with UI Policy with the conditions set to something like:


emp_workday_id is not empty (and)


emp_workday_id2 is not empty (and)


emp_workday_id is different than emp_workday_id2



however this condition in the UI Policy is not sticking for some reason.



So then I was thinking an onChange catalog client script.



any thoughts/suggestions?


patricklatella
Mega Sage

something like this?



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


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


          return;


    }




    //Ensure that the Workday ID fields match and if they do populate the employee details fields.


    var ID1 = g_form.getValue('emp_workday_id');


    var ID2 = g_form.getValue('emp_workday_id2');


   


    if


  (ID1 == ID2);


    else{


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


    }



}


patricklatella
Mega Sage

actually this one, which again this is not working, but I think you can see what I'm shooting for:



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


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


          return;


    }



    //Ensure that the Workday ID fields match and if they don't throw the message


    var ID1 = g_form.getValue('emp_workday_id');


    var ID2 = g_form.getValue('emp_workday_id2');


   


    if


  (ID1 == ID2);


    else{


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


    }



}