The CreatorCon Call for Content is officially open! Get started here.

How to remove the last value from a list collector on change of another variable?

Sujatha V M
Kilo Patron
Kilo Patron

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. 

Please mark this as helpful and accept it as a solution if this resolves your query.
Sujatha V.M.
4 REPLIES 4

Harshal Aditya
Mega Sage

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

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. 

Please mark this as helpful and accept it as a solution if this resolves your query.
Sujatha V.M.

Tushar
Kilo Sage
Kilo Sage

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

Sujatha V M
Kilo Patron
Kilo Patron

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. 

Please mark this as helpful and accept it as a solution if this resolves your query.
Sujatha V.M.