Get removed list collector item in client script

castle11
Tera Expert

Hi,

 

We have a list collector and we need to be determine what item was removed from it in an onchange client script. So for example, if the list has 'A', 'B', 'C' in it and we remove 'A', leaving 'B' and 'C, we need to be able to know that 'A' was removed in the client script. I would have though comparing the oldValue and newValue parameters would work, however, the oldValue parameter is always returning a blank string / nothing regardless of what's added/removed in the list collector. Is there any other way to access the old value or determine what value was removed?

1 ACCEPTED SOLUTION

I understand better now. Previous values won't be accessible the way you want. However, you could have a hidden list collector that acts the way you want for the comparison. Example:

  • List Collector A is displayed to the end user and is empty when the form is loaded
  • List Collector B is hidden from the end user and is empty when the form is loaded
  • List Collector A (current) changes and compares to List Collector B (previous)
  • At the end of the comparison, List Collector B is updated to match List Collector A (new previous)

 

The script I provided would need to be updated to be something like this:

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

	//Type appropriate comment here, and begin script below
	var oldSplit = g_form.getValue('old_list_collector').split(',');
	var newSplit = newValue.split(',');
	var removedValue = [];
	var addedValue = [];

	for(var i = 0; i < oldSplit.length; i++) {
		if(newSplit.indexOf(oldSplit[i]) == -1){
			removedValue.push(oldSplit[i]);
		}
	}

	for(var j = 0; j < newSplit.length; j++){
		if(oldSplit.indexOf(newSplit[j]) == -1) {
			addedValue.push(newSplit[j]);
		}
	}

	var text = 'oldValue=' + oldValue + '\nnewValue=' + newValue + '\nremovedValue=' + removedValue.toString() + '\naddedValue=' + addedValue.toString();
	g_form.setValue('u_text', text);

	g_form.setValue('old_list_collector', newValue);
}
Claude E. D'Amico, III - CSA

View solution in original post

8 REPLIES 8

Claude DAmico
Kilo Sage

oldValue should be what it was when the form loaded. newValue and current act the same, but newValue would only be the value without any additional context if I'm not mistaken. That being said, oldValue will always be blank for every change on the form when it was initially loaded as empty.

 

In a case like this, I prefer removing the commenting out /*|| newValue === ''*/ so I can capture changes to empty as well.

 

I created a table with just a list and string field for this. The onChange client script is:

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

   //Type appropriate comment here, and begin script below
   var text = 'oldValue=' + oldValue + '\nnewValue=' + newValue;
   g_form.setValue('u_text', text);
}

 

On a new record, oldValue is empty and newValue contains the sys_ids of the users added. Example:

oldValue=
newValue=6816f79cc0a8016401c5a33be04be441,62826bf03710200044e0bfc8bcbe5df1

 

After saving the record, I removed one of the users.

oldValue=6816f79cc0a8016401c5a33be04be441,62826bf03710200044e0bfc8bcbe5df1
newValue=62826bf03710200044e0bfc8bcbe5df1

 

And here is with an updated onChange script to check for added or removed values:

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

	//Type appropriate comment here, and begin script below
	var oldSplit = oldValue.split(',');
	var newSplit = newValue.split(',');
	var removedValue = [];
	var addedValue = [];

	for(var i = 0; i < oldSplit.length; i++) {
		if(newSplit.indexOf(oldSplit[i]) == -1){
			removedValue.push(oldSplit[i]);
		}
	}

	for(var j = 0; j < newSplit.length; j++){
		if(oldSplit.indexOf(newSplit[j]) == -1) {
			addedValue.push(newSplit[j]);
		}
	}

	var text = 'oldValue=' + oldValue + '\nnewValue=' + newValue + '\nremovedValue=' + removedValue.toString() + '\naddedValue=' + addedValue.toString();
	g_form.setValue('u_text', text);
}

Removed value:

oldValue=6816f79cc0a8016401c5a33be04be441,62826bf03710200044e0bfc8bcbe5df1
newValue=62826bf03710200044e0bfc8bcbe5df1
removedValue=6816f79cc0a8016401c5a33be04be441
addedValue=

Added value:

oldValue=6816f79cc0a8016401c5a33be04be441,62826bf03710200044e0bfc8bcbe5df1
newValue=6816f79cc0a8016401c5a33be04be441,62826bf03710200044e0bfc8bcbe5df1,be82abf03710200044e0bfc8bcbe5d1c
removedValue=
addedValue=be82abf03710200044e0bfc8bcbe5d1c

Added and Removed values:

oldValue=6816f79cc0a8016401c5a33be04be441,62826bf03710200044e0bfc8bcbe5df1
newValue=62826bf03710200044e0bfc8bcbe5df1,be82abf03710200044e0bfc8bcbe5d1c
removedValue=6816f79cc0a8016401c5a33be04be441
addedValue=be82abf03710200044e0bfc8bcbe5d1c

Claude E. D'Amico, III - CSA

Thanks for your response.

So this is only for after the record/form is submitted?

I need to be able to capture the old value live before the form is submitted - see my response to Jim Coyne for some extra context

I understand better now. Previous values won't be accessible the way you want. However, you could have a hidden list collector that acts the way you want for the comparison. Example:

  • List Collector A is displayed to the end user and is empty when the form is loaded
  • List Collector B is hidden from the end user and is empty when the form is loaded
  • List Collector A (current) changes and compares to List Collector B (previous)
  • At the end of the comparison, List Collector B is updated to match List Collector A (new previous)

 

The script I provided would need to be updated to be something like this:

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

	//Type appropriate comment here, and begin script below
	var oldSplit = g_form.getValue('old_list_collector').split(',');
	var newSplit = newValue.split(',');
	var removedValue = [];
	var addedValue = [];

	for(var i = 0; i < oldSplit.length; i++) {
		if(newSplit.indexOf(oldSplit[i]) == -1){
			removedValue.push(oldSplit[i]);
		}
	}

	for(var j = 0; j < newSplit.length; j++){
		if(oldSplit.indexOf(newSplit[j]) == -1) {
			addedValue.push(newSplit[j]);
		}
	}

	var text = 'oldValue=' + oldValue + '\nnewValue=' + newValue + '\nremovedValue=' + removedValue.toString() + '\naddedValue=' + addedValue.toString();
	g_form.setValue('u_text', text);

	g_form.setValue('old_list_collector', newValue);
}
Claude E. D'Amico, III - CSA

Thanks again, that makes sense. I'll give it a try and let you know