Append the output of two client scripts.

Shidhi
Tera Contributor

Hi,

 

I have two checkboxes Server and SPM Test Names if these checkboxes are true then I need to populate those names in a new field.

I have written individual Onchange client scripts so either the Server or SPM Test Name is getting placed into the variable but I want to have both names(Server,SPM Test Names) to be placed if both checkboxes are selected.

Please find below my code:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    if (newValue == 'true') {
        g_form.setValue('request_type', 'Server Names');
    }
   else
   g_form.setValue('request_type', ' ');
}

 

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
   if(newValue == 'true'){
	g_form.setValue('request_type', 'SPM Test Name');
   }
   else
   g_form.setValue('request_type', ' ');
}


Please help. Thank you!

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

If the Catalog Item variable names are 'server' and 'spm_test_names', something like this should work for server onChange:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    if (newValue == 'true') {
        if (g_form.getValue('spm_test_names') == 'true') {
            g_form.setValue('request_type', 'Server Names SPM Test Name'); 
        } else {       
            g_form.setValue('request_type', 'Server Names');
        }
    } else if (g_form.getValue('spm_test_names') == 'true') {
        g_form.setValue('request_type', 'SPM Test Name');
    } else {
       g_form.setValue('request_type', ' ');
    }
}

If 'Server Names' and 'SPM Test Name' are not literal values you can use a + to concatenate within the setValue...

View solution in original post

9 REPLIES 9

Kieran Anson
Kilo Patron

A lesser used API is the onUserChangeValue which allows you to setup a global listener to field changes. Below code is untested, but shows the usefulness of this function.

 

function onLoad() {
    var onChangeHandler = function(fieldName, originalValue, newValue) {

		//Store an array of field names we want to watch
        var fieldsToWatch = ['server', 'spm_test_names'];

		//If the updated field isn't one we care about, do nothing
        if (!fieldsToWatch.includes(fieldName))
            return;

		//Based on the value of the watched fields, create a csv-text of them if true
        var resultText = fieldsToWatch.map(function(fieldName) {

			if(g_form.getValue(fieldName) == 'true')
				return g_form.getLabelOf(fieldName);

        }, '').join(',');

		g_form.setValue('request_type' , resultText);

    };

    var unregister = g_form.onUserChangeValue(onChangeHandler);
    //you can call unregister() if you want to stop the behaviour.

}

Brad Bowman
Kilo Patron
Kilo Patron

If the Catalog Item variable names are 'server' and 'spm_test_names', something like this should work for server onChange:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    if (newValue == 'true') {
        if (g_form.getValue('spm_test_names') == 'true') {
            g_form.setValue('request_type', 'Server Names SPM Test Name'); 
        } else {       
            g_form.setValue('request_type', 'Server Names');
        }
    } else if (g_form.getValue('spm_test_names') == 'true') {
        g_form.setValue('request_type', 'SPM Test Name');
    } else {
       g_form.setValue('request_type', ' ');
    }
}

If 'Server Names' and 'SPM Test Name' are not literal values you can use a + to concatenate within the setValue...

Hi @Brad Bowman  

 

Only Server Names is getting printed not SPM Test name though i have selected. Could you fix this. I have used the same code.

 

Thank you!

Is the SPM variable name 'spm_test_names' or have you replaced it in your script?  You will need a similar script onChange of the SPM variable since either can be checked in any order.  This SPM script has to check the server variable value, so replace 'server' if that is not the variable name:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    if (newValue == 'true') {
        if (g_form.getValue('server') == 'true') {
            g_form.setValue('request_type', 'Server Names SPM Test Name'); 
        } else {       
            g_form.setValue('request_type', 'SPM Test Name');
        }
    } else if (g_form.getValue('server') == 'true') {
        g_form.setValue('request_type', 'Server Names');
    } else {
       g_form.setValue('request_type', ' ');
    }
}

Rajesh Chopade1
Mega Sage

Hi @Shidhi 

Use a single onChange function that checks the state of both checkboxes and updates the target field accordingly.

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

    // Get the values of both checkboxes
    var serverChecked = g_form.getValue('server_checkbox'); // Replace with your actual checkbox field name
    var spmTestChecked = g_form.getValue('spm_test_checkbox'); // Replace with your actual checkbox field name

    var requestType = '';

    // Check the state of the Server checkbox
    if (serverChecked == 'true') {
        requestType += 'Server Names';
    }

    // Check the state of the SPM Test Name checkbox
    if (spmTestChecked == 'true') {
        if (requestType) {
            requestType += ', '; // Add a comma if there's already a value
        }
        requestType += 'SPM Test Name';
    }

    // Set the combined value to the request_type field
    g_form.setValue('request_type', requestType);
}

 

i hope my answer helps you to resolve your issue, if yes please mark my answer helpful and correct.

thank you

rajesh