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

Show specific fields based on selections from list field

y-allen
Tera Contributor

I am working on a custom solution for my customer. On a form, when users make selections in a reference list field, we have specific fields that we want to appear. Some of the fields grab values from the referenced table and some are blank, allowing the user to input their own value. If the user removes a selection, we want the field to disappear AND clear any value that was there. There are calculations that run and we don't want those hidden values to be calculated.

 

I have successfully achieved this with client scripts, however, the last field remains displayed on the form after it is deselected. Saving is required to remove the form but even after saving, the value remains as evident by running the calculations and seeing that the numbers displayed in the input fields do not add up to the final value. No matter what the last field is, if it is last, it stays.

 

Can any assistance be provided in figuring out how to get this last field to operate as intended?

 

I have tried using a loop although I am not confident my ability to accurately script that. I have also tried the if statement you see at the very bottom of the script provided so that if "newValue" does not contain any sys_id's, then setDisplay "fields" false and clearValues for all "fields".

 

Thanks in advance.


function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {

        return;
    }

    //If selected, display field and if removed, clear field value.
    if (newValue.indexOf('025e6c32a1be46107f616f5e9b422469') !== -1) {
        g_form.setDisplay('field1', true);
    } else {
        g_form.setDisplay('field1', false);
        g_form.setValue('field1', '');
    }

    if (newValue.indexOf('10636426bdb646107f619a817d64165d') !== -1) {
        g_form.setDisplay('field2', true);
    } else {
        g_form.setDisplay('field2', false);
        g_form.setValue('field2', '');
    }

    if (newValue.indexOf('64542826bdb646107f619a817d64162f') !== -1) {
        g_form.setDisplay('field3', true);
    } else {
        g_form.setDisplay('field3', false);
        g_form.setValue('field3', '');
    }

    if (newValue.indexOf('6aae2072a1be46107f616f5e9b42249a') !== -1) {
        g_form.setDisplay('field4', true);
    } else {
        g_form.setDisplay('field4', false);
        g_form.setValue('field4', '');
    }

    if (newValue.indexOf('a2b32826bdb646107f619a817d641684') !== -1) {
        g_form.setDisplay('field5', true);
    } else {
        g_form.setDisplay('field5', false);
        g_form.setValue('field5', '');
    }

    if (newValue.indexOf('bd13a026bdb646107f619a817d6416bf') !== -1) {
        g_form.setDisplay('field6', true);
    } else {
        g_form.setDisplay('field6', false);
        g_form.setValue('field6', '');
    }

    if (newValue.indexOf('ed83e026bdb646107f619a817d641619') !== -1) {
        g_form.setDisplay('field7', true);
    } else {
        g_form.setDisplay('field7', false);
        g_form.setValue('field7', '');
    }

    //If newValue contains no sys_id's, clear fields from form
    var fields = [
        'field1',
        'field2',
        'field3',
        'field4',
        'field5',
        'field6',
        'field7'
    ];

    if (newValue.indexOf('')) {
        g_form.setDislay(fields, false);
        g_form.clearValue(fields);
    }

    alert(newValue);

}
1 ACCEPTED SOLUTION

Replace the below code:

 

if (newValue.indexOf('')) {
        g_form.setDislay(fields, false);
        g_form.clearValue(fields);
    }

 

with:

 

if (newValue == '')) {
for(var i=0; i<fields.length; i++)
{
g_form.setDisplay(fields[i], false);
        g_form.clearValue(fields[i]);
}
}

 

 

Just remember that you need to update at the beginning of the client script to run it even when newValue is empty... like this:

 

if (isLoading) { //Run even if new Value is empty
        return;
    }

 

View solution in original post

3 REPLIES 3

y-allen
Tera Contributor

I should add that this is an onChange client script set to monitor the list field.

Replace the below code:

 

if (newValue.indexOf('')) {
        g_form.setDislay(fields, false);
        g_form.clearValue(fields);
    }

 

with:

 

if (newValue == '')) {
for(var i=0; i<fields.length; i++)
{
g_form.setDisplay(fields[i], false);
        g_form.clearValue(fields[i]);
}
}

 

 

Just remember that you need to update at the beginning of the client script to run it even when newValue is empty... like this:

 

if (isLoading) { //Run even if new Value is empty
        return;
    }

 

It looks like the corrections to the isLoading at the beginning of he script resolved the issue. Thank you very much for your assistance!