How to remove the last value from a list collector on change of another variable?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-27-2023 08:58 AM
Hello Team,
I have variable A (list collector) which holds a default value. I would like to add another value to the same variable A on change of another variable.
If 'yes' - Variable A [Default Value + New Value]
If 'no' - Variable A [Default Value]
That new value is a 'sys_id'.
I'm trying with slice syntax and it erases all the values on call of script include.
Sujatha V.M.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-27-2023 10:41 AM
Hi @Sujatha V M ,
Hope you are doing well.
Please try the below code -
// If Yes
var list1 = g_form.getValue("test_list");
var arr = [];
arr = list1.split(",");
arr.push("replace with the sys_id to push"); // replace with the value to push
g_form.setValue("test_list",arr); // default value + newValue
Please mark this response as correct or helpful if it assisted you with your question.
Regards,
Harshal
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2023 09:27 PM
Thanks for the response. But there is slight change on the requirement.
Instead of a default value in the variable A, its a dynamic value now based on another variable.
i.e Variable X is selected -> Variable A [list collector] needs to be populated.
then if "Yes" on variable Y -> Append the new value to the Variable A along with the existing one.
If "No" on variable Y -> Remove the appended value from Variable A and keep the existing one.
Sujatha V.M.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-27-2023 10:49 AM
HI @Sujatha V M
A client-script prototype for your requirement, please update the variables as required -
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
// Get the 'yes/no' variable (assuming its name is 'yesNoVariable')
var yesNoVariable = g_form.getValue('yesNoVariable');
if (yesNoVariable === 'yes') {
// Get the current value of the list collector variable A
var currentValue = g_form.getValue('variableA');
// Check if the new value is not already present in the list
if (!currentValue.includes(newValue)) {
// Add the new value to the existing value, separated by a comma
var updatedValue = currentValue + ',' + newValue;
// Set the updated value back to the list collector variable A
g_form.setValue('variableA', updatedValue);
}
} else if (yesNoVariable === 'no') {
// If 'no' is selected, set the list collector variable A to its default value
var defaultValue = 'default_value'; // Replace 'default_value' with the actual default value
g_form.setValue('variableA', defaultValue);
}
}
Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!
Regards,
Tushar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2023 09:25 PM
Thank you for the response. But there is slight change on the requirement.
Instead of a default value in the variable A, its a dynamic value now based on another variable.
i.e Variable X is selected -> Variable A [list collector] needs to be populated.
then if "Yes" on variable Y -> Append the new value to the Variable A along with the existing one.
If "No" on variable Y -> Remove the appended value from Variable A and keep the existing one.
Sujatha V.M.