- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-20-2024 11:46 AM
Hi !!
I am facing issue on how to copy values from left slushbucket in a list collector field to a multi line text field in a record producer using catalog client script.
I have a list collector field which contains some state values , as soon as the user selects some values ,I should exclude those selected values and copy remaing values (left slushbucket) values in a multi line text field.
My list collector field is referenced to select box variable that contains choice values on question choices table.
I am unable to populate the values on change of the list collector variable using catalog client script.
Please do help me with the code.
From the above screenshot , i should be able to populate display users field with all other values from left slushbucket excluding Hawaii & Michigan as soon as the user selects them in list collector variable.
Caltalog Client script:
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-20-2024 08:47 PM
Hi @Pravallika1
There are a few adjustments needed. Below is the revised version of your script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var initialChoices = ['dc8b769893a0021035e6b2ddfaba10cc','1ecb365893a0021035e6b2ddfaba1009','4bebf65893a0021035e6b2ddfaba104e','fc2cfe9893a0021035e6b2ddfaba10e4','7d3cb6909320021035e6b2ddfaba10aa','2e5c3a9893a0021035e6b2ddfaba1066','b66c769893a0021035e6b2ddfaba10d6','ea8cf6d893a0021035e6b2ddfaba101f','ee9cf69893a0021035e6b2ddfaba10c8'];
var selectedChoices = g_form.getValue('test_users');
var selectedChoicesArray = selectedChoices.split(',');
var states = [];
for (var i = 0; i < initialChoices.length; i++) {
if (selectedChoicesArray.indexOf(initialChoices[i]) == -1) {
states.push(initialChoices[i]);
}
}
var gaStates = new GlideAjax('u_AjaxUtils');
gaStates.addParam('sysparm_name', 'getStatesDiff');
gaStates.addParam('sysparm_states', states);
gaStates.getXML(SetfieldValues);
function SetfieldValues(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('display_users', answer);
}
}
Script Include:
var AjaxUtils = Class.create();
AjaxUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getStatesDiff: function() {
var states = this.getParameter('sysparm_states').split(',');
var traineeEmailCollection = [];
for (var i = 0; i < states.length; i++) {
var gr = new GlideRecord('question_choice');
if (gr.get(states[i])) {
traineeEmailCollection.push(gr.text);
}
}
return traineeEmailCollection.join('\n');
}
});
HIT Helpful & Accept Solution !!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-21-2024 08:09 AM
Thank you so much all of you!!
These codes really worked for me @Akshay Gupta2 @Sohithanjan G
But i am facing an issue when selected slushbucket is empty i.e,
If we add two values to selected slushbucket (e.g,Alaska , Alabama) and if i again move Alabama to available slushbucket the Display users field is updated correctly , then if i send that last value i.e, Alaska and check the Display users field, the Alaska value is not updated.
Please do provide a solution on this.
Please follow the screenshots:
1.
2.In this screenshot you can see 'Alabama' is updated in display users field
3.In this screenshot you can see , even after i move back Alaska display users field is not updated-
Please help me on this.
Thanks in advance!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-21-2024 10:12 AM - edited ‎02-21-2024 10:13 AM
Hi @Pravallika1 ,
It seems like there might be an issue with the logic when selectedChoices is empty. Let's adjust the code to handle this scenario more effectively:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === undefined || newValue === null) {
// Clear display_users field when no selection is made
g_form.setValue('display_users', '');
return;
}
var initialChoices = ['dc8b769893a0021035e6b2ddfaba10cc','1ecb365893a0021035e6b2ddfaba1009','4bebf65893a0021035e6b2ddfaba104e','fc2cfe9893a0021035e6b2ddfaba10e4','7d3cb6909320021035e6b2ddfaba10aa','2e5c3a9893a0021035e6b2ddfaba1066','b66c769893a0021035e6b2ddfaba10d6','ea8cf6d893a0021035e6b2ddfaba101f','ee9cf69893a0021035e6b2ddfaba10c8'];
var selectedChoices = g_form.getValue('test_users');
var selectedChoicesArray = selectedChoices ? selectedChoices.split(',') : [];
var states = [];
for (var i = 0; i < initialChoices.length; i++) {
if (selectedChoicesArray.indexOf(initialChoices[i]) == -1) {
states.push(initialChoices[i]);
}
}
var gaStates = new GlideAjax('u_AjaxUtils');
gaStates.addParam('sysparm_name', 'getStatesDiff');
gaStates.addParam('sysparm_states', states);
gaStates.getXML(SetfieldValues);
function SetfieldValues(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('display_users', answer);
}
}
Please HIT helpful & Mark as Accepted Solution !!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-22-2024 12:31 AM
@Sohithanjan G I shouldn't clear the display_users field when no selection is made instead I should populate all the states present in left slushbucket.
Please do help with code
Thanks in advance!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-22-2024 04:28 AM
@Sohithanjan G I also declared initialChoices array with sys ids in client script. Now i am unable to use gs.getProperty function in client script. Can you please help me how to code this without using sys ids in client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-21-2024 08:39 AM
Hi Guys!!
Thank you all so much for your response, it is really helpful to achieve my initial solution.
Please do help me with the continuation requirement which i posted in the question - To map values from list collector variable to a ch... - ServiceNow Community
Please do help me with code and solution.
Thanks in Advance!!