Build a variable string based on checkmarked variables

Bret Smith
Giga Guru

 

I would like to build a temporary variable based on what checkmark variables have been selected

 

 

If thrid_party_bidm == 'true'
add/concatenate 'BIMD ' to temp_variable

 

If thrid_party_cbms == 'true'
add/concatenate 'CBMS ' to temp_variable

 

If thrid_party_ccm_medcompass == 'true'
add/concatenate 'CCM-MedCompass ' to temp_variable

 

If third_party_interchange == 'true'
add/concatenate 'interChange ' to temp_variable

 

If thrid_party_pbms == 'true'
add/concatenate 'PBMS ' to temp_variable

 

If third_party_peak_pro == 'true'
add/concatenate 'PEAK Pro ' to temp_variable

 

If thrid_party_save == 'true'
add/concatenate 'SAVE ' to temp_variable

 

1 ACCEPTED SOLUTION

Community Alums
Not applicable

Hi @Bret Smith ,

 

Scenario 1 - If you need `temp_variable` to calculate, manipulate other variables, or perform actions on the client-side form, you can achieve this with the onChange client script for each checkbox variable (`thrid_party_bidm`, `thrid_party_cbms`, `thrid_party_ccm_medcompass`, `third_party_interchange`, `thrid_party_pbms`, `third_party_peak_pro`, `thrid_party_save`).

 

E.g., onChange client script for the thrid_party_bidm variable

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

    var tempVariable = g_form.getValue('temp_variable').split(',').filter(Boolean);

    if (g_form.getValue('thrid_party_bidm') === 'true') {
        if (tempVariable.indexOf('BIMD') === -1) {
            tempVariable.push('BIMD');
        }
    } else {
        if (tempVariable.indexOf('BIMD') !== -1) {
            tempVariable = tempVariable.filter(function(element) {
                return element !== 'BIMD';
            });
        }
    }

    g_form.setValue('temp_variable', tempVariable.join(', '));
}

 

Scenario 2 - If you just want to save the `temp_variable` variable and use it in Server-Side actions, you can achieve this functionality by using the onSubmit client script.

E.g.,

function onSubmit() {
    var tempVariable = '';

    // Check if each checkbox variable is selected, and concatenate to tempVariable accordingly
    if (g_form.getValue('thrid_party_bidm') === 'true') {
        tempVariable += 'BIMD ';
    }
    if (g_form.getValue('thrid_party_cbms') === 'true') {
        tempVariable += 'CBMS ';
    }
    if (g_form.getValue('thrid_party_ccm_medcompass') === 'true') {
        tempVariable += 'CCM-MedCompass ';
    }
    if (g_form.getValue('third_party_interchange') === 'true') {
        tempVariable += 'interChange ';
    }
    if (g_form.getValue('thrid_party_pbms') === 'true') {
        tempVariable += 'PBMS ';
    }
    if (g_form.getValue('third_party_peak_pro') === 'true') {
        tempVariable += 'PEAK Pro ';
    }
    if (g_form.getValue('thrid_party_save') === 'true') {
        tempVariable += 'SAVE ';
    }

    // Trim any trailing spaces
    tempVariable = tempVariable.trim();

    // Set the value of temp_variable field
    g_form.setValue('temp_variable', tempVariable);

    // Proceed with form submission
    return true;
}

 

View solution in original post

3 REPLIES 3

James Chun
Kilo Patron

Hi @Bret Smith,

 

I am assuming this will be used in a client script.

If so, try something like the below:

var variable_pair = {
	'third_party_bidm' : 'BIMD',
	'thrid_party_cbms ' : 'CBMS'
}//Append with your key-value pairs

var temp_variable = [];

for(var dict in variable_pair)
{
	if(g_form.getValue(dict) == 'true')
	{
		temp_variable.push( variable_pair[dict] );
	}
}

return temp_variable.join(',');

 

Cheers

 

Community Alums
Not applicable

Hi @Bret Smith ,

 

Scenario 1 - If you need `temp_variable` to calculate, manipulate other variables, or perform actions on the client-side form, you can achieve this with the onChange client script for each checkbox variable (`thrid_party_bidm`, `thrid_party_cbms`, `thrid_party_ccm_medcompass`, `third_party_interchange`, `thrid_party_pbms`, `third_party_peak_pro`, `thrid_party_save`).

 

E.g., onChange client script for the thrid_party_bidm variable

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

    var tempVariable = g_form.getValue('temp_variable').split(',').filter(Boolean);

    if (g_form.getValue('thrid_party_bidm') === 'true') {
        if (tempVariable.indexOf('BIMD') === -1) {
            tempVariable.push('BIMD');
        }
    } else {
        if (tempVariable.indexOf('BIMD') !== -1) {
            tempVariable = tempVariable.filter(function(element) {
                return element !== 'BIMD';
            });
        }
    }

    g_form.setValue('temp_variable', tempVariable.join(', '));
}

 

Scenario 2 - If you just want to save the `temp_variable` variable and use it in Server-Side actions, you can achieve this functionality by using the onSubmit client script.

E.g.,

function onSubmit() {
    var tempVariable = '';

    // Check if each checkbox variable is selected, and concatenate to tempVariable accordingly
    if (g_form.getValue('thrid_party_bidm') === 'true') {
        tempVariable += 'BIMD ';
    }
    if (g_form.getValue('thrid_party_cbms') === 'true') {
        tempVariable += 'CBMS ';
    }
    if (g_form.getValue('thrid_party_ccm_medcompass') === 'true') {
        tempVariable += 'CCM-MedCompass ';
    }
    if (g_form.getValue('third_party_interchange') === 'true') {
        tempVariable += 'interChange ';
    }
    if (g_form.getValue('thrid_party_pbms') === 'true') {
        tempVariable += 'PBMS ';
    }
    if (g_form.getValue('third_party_peak_pro') === 'true') {
        tempVariable += 'PEAK Pro ';
    }
    if (g_form.getValue('thrid_party_save') === 'true') {
        tempVariable += 'SAVE ';
    }

    // Trim any trailing spaces
    tempVariable = tempVariable.trim();

    // Set the value of temp_variable field
    g_form.setValue('temp_variable', tempVariable);

    // Proceed with form submission
    return true;
}

 

This is perfect!!!!! Thank you so much