How to check uniqueness of field in client callable script include.

shalani1
Tera Contributor

Hi,
I have a requirement to check uniqueness of field in a form and populate a error message as it should work in form and list as well. 
Requirement:
There are two tables in which  second table consist of one field called "id" which is referencing to incident table so whenever we fill the id it should be unique and not repeated incident number. Whenever id changes it should check for uniqueness and populate error message if its not unique.

Script include:
getID: function(){

var grid= this.getParameter('sysparm_id');

var a=new GlideRecord('table1');

a.addQuery(''number',grid);

a.query();

if(a.next()){

return;

}

},
onchange client script:
onchange: number
if(isLoading || newValue===''){

return;

}

var gx=new GlideAjax('script include name');

gx.addParam("sysparm_name","getID");

gx.addParam("sysparm_id",newValue);

gx.getXML(response1);

}

function response1(response){

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

if(ans){

g_form.ErrorMessage("Must be unique");

}

}

Please can anyone help me to understand where the script going wrong.
Thanks in Advance!

Thanks,
Shalani

3 REPLIES 3

Brad Bowman
Kilo Patron
Kilo Patron

Your GR is probably returning every record, or no records, due to mismatched (double and single) quotes in the addQuery line.  If the field that is changing is a reference type, then newValue will be a sys_id, so make sure that matches 'number', or change this to a field on table 1 that is a reference.  You should also always return something, so your SI would look more like this:

 

var answer = '';
var grid= this.getParameter('sysparm_id');
var a=new GlideRecord('table1');
a.addQuery('number',grid); 
a.query();
if (a.next()) {
    answer = 'true';
}
return answer;

 

Once you get this working on the form, you can try the same onCellEdit for a list view, but that's probably going to be easier with a before Update Business Rule containing a similar script, or calling the same Script Include. 

 

Thanks for the response!
I have tried below script but not working, still its not checking for uniqueness in the form. Please find below script which is modified:

Script include:
getID: function(){

var answer  ='';

var grid= this.getParameter('sysparm_id');

var a=new GlideRecord('table1');

a.addQuery('number',grid);

a.query();

if(a.next()){

answer='true';

}

return answer;

 

},
onchange client script:
onchange: number
if(isLoading || newValue===''){

return;

}

var gx=new GlideAjax('script include name');

gx.addParam("sysparm_name","getID");

gx.addParam("sysparm_id",newValue);

gx.getXML(response1);

}

function response1(response){

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

if(ans){

g_form.ErrorMessage("Must be unique");

}

}



Luc_Sutherland
Tera Expert

In your response function, I believe you need to change the attribute from "ans" to "answer":

 

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

 

If this doesn't work, I would suggest adding logging so you can see the values of each variable before and after you use them.