Auto populate field based on other field choice values in the form

Avinash72
Tera Contributor

In the form I have 10 choice fields with choices 'Green' 'Yellow' 'Red'.

There is a new field called Overall Status which should auto populate  based on above 10 fields choice values.

When only there is 2 or less Yellow  with No Red - Populate as Minor

When there is 3 or more Yellow or 1 or more  Red  - Populate as Major

Minor and Major are choice values of Overall Status fields.

 

I have gone through on Load client script but struck in the middle.

 

Any help is truly appreciated.

 

 

1 ACCEPTED SOLUTION

Here's one way to do that - substituting your field names and choice values

function onLoad() {
	var redcnt = 0;
	var yellowcnt = 0;
	var field1 = g_form.getValue('field_1_name');
	var field2 = g_form.getValue('field_2_name');
	var field3 = g_form.getValue('field_3_name');
    //...same for all 10 fields
	var fieldArr = [field1, field2, field3]; //include all 10 variable names
	for (var i=0; i<fieldArr.length; i++) {
		if (fieldArr[i] == 'red') {
			redcnt++;
		} else if (fieldArr[i] == 'yellow') {
			yellowcnt++;
		}
	}  
	if (redcnt > 0 || yellowcnt > 2){
		g_form.setValue('overall_status_field_name', 'major');
	} else if (redcnt == 0 && yellowcnt < 3) {
		g_form.setValue('overall_status_field_name', 'minor');
	}
}

View solution in original post

6 REPLIES 6

Brad Bowman
Kilo Patron
Kilo Patron

Post your script attempt, and we'll get it sorted.  Do you really want this to happen on form/record load, or behind the scenes when one of the 10 choice field values changes?

Hi Brad,

 

That 10 fields will also auto populate, there is another scenario for that. So on load client script is preferred.

Avinash72_0-1724074386327.png

 

 

Here's one way to do that - substituting your field names and choice values

function onLoad() {
	var redcnt = 0;
	var yellowcnt = 0;
	var field1 = g_form.getValue('field_1_name');
	var field2 = g_form.getValue('field_2_name');
	var field3 = g_form.getValue('field_3_name');
    //...same for all 10 fields
	var fieldArr = [field1, field2, field3]; //include all 10 variable names
	for (var i=0; i<fieldArr.length; i++) {
		if (fieldArr[i] == 'red') {
			redcnt++;
		} else if (fieldArr[i] == 'yellow') {
			yellowcnt++;
		}
	}  
	if (redcnt > 0 || yellowcnt > 2){
		g_form.setValue('overall_status_field_name', 'major');
	} else if (redcnt == 0 && yellowcnt < 3) {
		g_form.setValue('overall_status_field_name', 'minor');
	}
}

Awesome, Thank you very much Sir.