Add/Remove values from a List collector based on Check box fields.

Nipan1
Tera Contributor

Hi, 

 

I have a requirement to populates(add) value in a list collector's value/ remove the value of the list collector field, based on 3 check box fields. 

 

I have tried Script include and onChange client script and also tried to modify through Reference qualifier of the list collector field. But nothing is working. 

 

Please suggest me a better way to achieve it.

 

Reference picture of my requirement: 

  • If the user choose Digital Engg app in the Application field, the check boxes will appear. 
  • Then if user checks any check box, it will add/remove the value in Application field. 

Nipan1_0-1725562681234.png

 

Thanks

 

1 ACCEPTED SOLUTION

Good job with the logs.  You were very close - just a misplaced } that was causing there to be no 'else' to the if (newValue == 'true').  Then you would have discovered that you want to use splice to remove an element from an array in this case, not slice which creates a new array with the removed elements.  Here's the working if block:

if (newValue == 'true') {
    if (appVal.indexOf(jira_sysid) === -1) {
        appVal.push(jira_sysid);
        alert('added: ' + jira_sysid);
    }
} else {
    var index = appVal.indexOf(jira_sysid);
    alert('index found at: ' + index);
    if (index != -1) {
        alert('removing');
	    appVal.splice(index, 1);
    } else {
        alert('value not found for removal!');
    }
}

 

View solution in original post

5 REPLIES 5

Brad Bowman
Kilo Patron
Kilo Patron

An onChange client script when each checkbox changes is the way to go.  Get one nailed down, then copy it to trigger on the other two.  All you need to do is set the value of the list collector to a comma-separated list of sys_ids for records on the list table.  Post your script attempt if you are still stuck.

Hi @Brad Bowman ,

 

Thanks for the reply!

 

I wrote the onChange client script as you said and Adding value to the List collector field is working. But the removing part is not working. It's not entering to the else part (not even getting any alerts from else part).

 

Pasted my code below. Can you please find my fault.

 

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var getCollector = g_form.getValue('applications');
    var jira_sysid = 'ea7b1a23dba4ca5855afd383f39619ea'; //'Jira Cloud'

    alert('value: ' + newValue);
    alert('app  field before: ' + getCollector);

    var appVal = getCollector.split(',');

    alert('app  field current: ' + getCollector);
    alert('value to add/remove: ' + newValue);

    if (newValue == 'true') {
        if (appVal.indexOf(jira_sysid) === -1) {
            appVal.push(jira_sysid);
            alert('added: ' + jira_sysid);

        } else {
            var index = appVal.indexOf(jira_sysid);
            alert('index found at: ' + index);
            if (index !== -1) {
                appVal.slice(index, 1);
            } else {
                alert('value not found for removal!');
            }
        }
    }
    var newAppVal = appVal.join(',');
    g_form.setValue('applications', newAppVal);
    alert('app field after: ' + newAppVal);
}

 

 
 

Good job with the logs.  You were very close - just a misplaced } that was causing there to be no 'else' to the if (newValue == 'true').  Then you would have discovered that you want to use splice to remove an element from an array in this case, not slice which creates a new array with the removed elements.  Here's the working if block:

if (newValue == 'true') {
    if (appVal.indexOf(jira_sysid) === -1) {
        appVal.push(jira_sysid);
        alert('added: ' + jira_sysid);
    }
} else {
    var index = appVal.indexOf(jira_sysid);
    alert('index found at: ' + index);
    if (index != -1) {
        alert('removing');
	    appVal.splice(index, 1);
    } else {
        alert('value not found for removal!');
    }
}

 

Hi, it works. 😊 Thanks @Brad Bowman