How to get old value of list collector type variable in onchange client script

v-paulp
Tera Contributor

Hi All,

How to get Old value of a list collector type variable in onchange client script? I am using below client script but old value always return empty.

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var value = g_form.addInfoMessage(oldValue);
	g_form.addInfoMessage('with get value ' + g_form.getValue('region'));
    var oldValuesArray = oldValue ? oldValue.split(',') : [];
    var newValuesArray = newValue ? newValue.split(',') : [];
    var removedValues = oldValuesArray.filter(function(item) {
        return newValuesArray.indexOf(item) === -1;
    });
    if (removedValues.length > 0) {
        g_form.clearValue('country_tableau');
		g_form.addInfoMessage(removedValues + ' ' + oldValuesArray + ' ' + newValuesArray);
    }else{
		g_form.addInfoMessage('not working' + ' ' + oldValuesArray + ' ' + newValuesArray);
	}

}

 

5 REPLIES 5

Brad Bowman
Mega Patron

oldValue will always be the value when the form was loaded.  Are you using this on the request form, RITM, Catalog Task, or all of these?  If you say more about what you're trying to accomplish, we can suggest alternate approaches.

Hi @Brad Bowman  ,
There is two list collector type variable region and country. Based on the value I have selected on region the country value populated. Now I want if user remove any value from region variable associated country also should also auto removed automatically.

It sounds like you want to filter the country list collector possible selections based on the region.  If these two fields are related in your environment you can do this with a reference qualifier on the country variable.  Be sure to also add a variable attribute:

 ref_qual_elements=v_region

where 'v_region' is the name of the region list collector variable.

sivasankaris
Tera Guru

Hi @v-paulp ,

List Collector variables do not natively track the oldValue parameter in the same way a simple string or choice field does. When the onChange script triggers, the platform often passes an empty string or the current value into that parameter, making it difficult to detect exactly what was added or removed.

 

Correct Approach 

You need to store the previous value yourself and compare it on the next change.

Step 1: Create a variable to store the old value.

  • Variable name: prev_region

  • Type: Single Line Text

 

Step 2: Update your onChange client script

 

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) {
        // Store initial value
        g_form.setValue('prev_region', newValue);
        return;
    }

    var prevValue = g_form.getValue('prev_region') || '';
    var oldValuesArray = prevValue ? prevValue.split(',') : [];
    var newValuesArray = newValue ? newValue.split(',') : [];

    var removedValues = oldValuesArray.filter(function (item) {
        return newValuesArray.indexOf(item) === -1;
    });

    if (removedValues.length > 0) {
        g_form.clearValue('country_tableau');
        g_form.addInfoMessage('Removed: ' + removedValues.join(', '));
    }

    // Update stored value for next change
    g_form.setValue('prev_region', newValue);
}

 

If this works, please mark it as helpful and please accept my solution..

Best Regards

SIVASANKARI S