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

Bhavani Shankar
Tera Guru

Hey @Avinash72 ,

 

Can you try to create a custom data lookup table. So, this functionality would be similar to the priority field auto population in incident form based on impact and urgency fields. 

 

dl_u_priority is the lookup table for priority field, go through it and see if it makes sense to use a similar approach for your use case.

 

If you opt for a custom data lookup here is the docs link Custom data lookup (servicenow.com) 

 

 

Regards,
Bhavani Shankar
Linked In

Thank you for your reply.