Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to set Catalog field value based on 2 other variables

Aaron Duncan
Mega Sage

I have a requirement where based on 2 variables in the same catalog item, it would set a value into a 3rd variable. This needs to be able to update as the values on the 1st 2 variables change.  The 3rd variable has 2 values that can be set into it. 

 

What would be the best way to make this happen? 

1 ACCEPTED SOLUTION

Steven Parker
Giga Sage

2 onChange Client Scripts for the first 2 variables.  Use the same script for each.  Script should look something like the below.  Just add more if statements as needed.  With 2 onChange scripts, this script would run when either of the first 2 variables changed, but only set field 3 when both field 1 and 2 were whatever you wanted them to be.

 

 

function onChange(control, oldValue, newValue, isLoading) {

//Get the first 2 variable values
var field1Val = g_form.getValue('field1');
var field2Val = g_form.getValue('field2');
	
	//Set variable 3 to whatever you need it to be based on the first 2 variables
    if (field1Val == 'Whatever Value' && field2Val == 'Whatever Value'){
       g_form.setValue('field3','First Value');
    }

    if (field1Val == 'Whatever Next Value' && field2Val == 'Whatever Next Value'){
       g_form.setValue('field3','Second Value');
    }
    	
   
}

 

 

 


Please mark this response as correct and/or helpful if it assisted you with your question.
Steven

View solution in original post

2 REPLIES 2

Steven Parker
Giga Sage

2 onChange Client Scripts for the first 2 variables.  Use the same script for each.  Script should look something like the below.  Just add more if statements as needed.  With 2 onChange scripts, this script would run when either of the first 2 variables changed, but only set field 3 when both field 1 and 2 were whatever you wanted them to be.

 

 

function onChange(control, oldValue, newValue, isLoading) {

//Get the first 2 variable values
var field1Val = g_form.getValue('field1');
var field2Val = g_form.getValue('field2');
	
	//Set variable 3 to whatever you need it to be based on the first 2 variables
    if (field1Val == 'Whatever Value' && field2Val == 'Whatever Value'){
       g_form.setValue('field3','First Value');
    }

    if (field1Val == 'Whatever Next Value' && field2Val == 'Whatever Next Value'){
       g_form.setValue('field3','Second Value');
    }
    	
   
}

 

 

 


Please mark this response as correct and/or helpful if it assisted you with your question.
Steven