Try to build client & server script client for phonenumber existence during catalog item submission

JaruwanN
Tera Contributor

Description: I have one requirement from business to validate phone number against x_smcit_on_board_0_field_service_contractors, if it exists do not continue with catalog item submission and asking for new number, if not exists continue with catalog submission. I created the client script and server script but it does not seem to work. The answer also come as NULL. Please help.

Steps to reproduce:
## client script ##

function onSubmit() {

var formPhoneNumber = g_form.getValue('phone_number');
var ga = new GlideAjax("fieldservicecontractorsPhonenumberValidation"); //x_smcit_on_board_0_field_service_contractors
ga.addParam('sysparm_name', 'checkIfRecordExists');
ga.addParam('sysparm_field_value', formPhoneNumber);
ga.setScope('On Boarding');
ga.getXMLWait();

var response = ga.getAnswer();
if (response === 'exists') {
alert("Phone number already exists.");
g_form.showFieldMsg('phone_number', 'This phone number is already in use.', 'error');
return false;
}
return true;
}

## Server Script ##
var fieldservicecontractorsPhonenumberValidation = Class.create();
fieldservicecontractorsPhonenumberValidation.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

// Function to check if a record with specific values already exists
checkIfRecordExists: function() {
var fieldValue = this.getParameter('sysparm_field_value');
var recordExists = 'false';

var gr = new GlideRecord('x_smcit_on_board_0_field_service_contractors');
gr.addQuery('phone', fieldValue);
// Important for performance since we only need to know if one record exists
gr.setLimit(1);
gr.query();

if (gr.next()) {
return 'exists';
}

return 'not_exists';
},

type: 'fieldservicecontractorsPhonenumberValidation'
});

1 REPLY 1

GlideFather
Tera Patron

Hi @JaruwanN,

 

have you tried logging what the script include returns? above the return lines.. 

 

Also, just an idea but phone numbers might have different formats (e.g. 00420 versus +420), do you have any method how to simplify that? Also some people write it with spaces +420 123 456 789, some with brackets (123) 456 789 etc.... 

 

 

_____
100 % GlideFather experience and 0 % generative AI