Having trouble querying if a field contains a special character
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-20-2023 06:18 AM
Hi all,
I have a working catalog client script that takes the user from a reference field, it then takes the values from a field on the sys_user record called u_key_individual_of_partners, splits it using ';' and then displays the values in a select box. This all works perfectly fine.
However the issue I'm having is that I only want it to do this if the user has more than one value in the u_key_individual_of_partners field. I know that it does as we use the semi-colon ';' as a split value. So I need to add into the query that the u_key_individual_of_partners field contains a ; however when I add this to the query I get errors and the select box field isn't populated.
If I run it as a background script however it works fine so maybe it's a scripting error that cannot be done client-side?
I have tried adding the following into the query but all three have the same result (or there lack of).
gr.addQuery('u_key_individual_of_partners', 'CONTAINS', ";");
gr.addQuery('u_key_individual_of_partnersLIKE;');
gr.addEncodedQuery('u_key_individual_of_partnersLIKE;');
Any ideas where I may be going wrong - or is there a different way of doing it?
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
var id ='';
id = newValue;
//alert('sys_id of user : ' + id);
var gr = new GlideRecord("sys_user");
gr.addQuery("sys_id", id);
gr.query(myCallbackFunction); //Execute the query with callback function
//After the server returns the query recordset, continue here
function myCallbackFunction(gr){
while (gr.next()) { //While the recordset contains records, iterate through them
var keypart = gr.u_key_individual_of_partners;
var valArray = keypart.split('; '); // values will be in an array named valArray
for (var i=0 ; valArray.length >i; i++){
g_form.addOption('practice_needed', valArray[i], valArray[i]);
}
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-20-2023 06:32 AM
Hi @Liam Rhodes ,
Using GlideRecord on client side is not a good practice. Please use GlideAjax instead n check if it works or not.
sharing sample script on how to call it from client side
var ga = new GlideAjax('callerUtilsAjax');
ga.addParam('sysparm_name', 'getComputer');
ga.addParam('sysparm_caller', g_form.getValue('called_id'));
ga.getXMLAnswer(_handleResponse);
function _handleResponse(answer) {
console.log(answer);
}
Thanks,
Danish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-20-2023 01:00 PM
Thanks Danish,
I've now updated the client script to use GlideAjax instead. The parameters are being passed through to the script include fine and I'm getting the right response returned to the client script. I'm now having issues splitting the returned answer into an array - any idea what might help here? Should I perform the split in the Script Include and then pass that through?
Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
var id = newValue;
var ga = new GlideAjax('keyPartnerData');
ga.addParam('sysparm_name', 'getKeyPracticeCodes');
ga.addParam('sysparm_user', id);
ga.getXML(populatePCodes);
function populatePCodes(response){
var answer = JSON.parse(response.responseXML.documentElement.getAttribute("answer"));
//alert(answer.value);
var valArray = answer.split('; '); // values will be in an array named valArray
for (var i=0 ; valArray.length >i; i++){
g_form.addOption('practice_needed', valArray[i], valArray[i]);
}
}
}
Script Include:
var keyPartnerData = Class.create();
keyPartnerData.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getKeyPracticeCodes: function() {
var user = this.getParameter('sysparm_user');
var returnCode = {};
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', user);
gr.addQuery('u_key_individual_of_partnersLIKE;');
gr.query();
if (gr.next()){
returnCode.value = gr.getDisplayValue('u_key_individual_of_partners');
return JSON.stringify(returnCode);
} else {
return;
}
},
type: 'keyPartnerData'
});