OnChange for multiple fields

Akshay Bhaskar
Kilo Sage

Hi Everyone, 

The problem record in my instance have the fields - Assignment Group, Priority and Review Date. My requirement is to make the comments mandatory when one of the above value changes. I've written three onChange client scripts for and the script in general is - 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    //Type appropriate comment here, and begin script below
    if ((newValue != oldValue) && (oldValue != '')) {
        g_form.setMandatory('comments', true);
        g_form.addInfoMessage("Please justify within comments, the update made to the priority.");
    } else{
        g_form.setMandatory('comments', false);
      }
}


The use case scenario that has failed is - 
1. Assume assignment group's original value = GroupABC. User has updated it to GroupXYZ (Result - mandatory comments)
2. Assume priority is 4-Low and is updated to 3-Medium (Result - mandatory comments)
3. If the user updates the assignment group back to GroupABC, the comments become non-mandatory and allows the form to be saved without any justification for the change in priority. 

Are there any workarounds to address this issue? 


Regards,
Akshay

1 ACCEPTED SOLUTION

@Akshay Bhaskar 

you can know which field got changed based on that group.Changed flag and based on that show respective messages using multiple if else statements.

I hope you will be able to incorporate those changes.

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

why not use single onSubmit and validate that?

function onSubmit() {
	var group = g_form.getControl('assignment_group');
	var priority = g_form.getControl('priority');
	var reviewDate = g_form.getControl('review_date');

	//See if the 'changed' attribute on either field is true
	if(group.changed || priority.changed || reviewDate.changed){
		if(g_form.getValue('comments') == ''){
			g_form.setMandatory('comments', true);
			g_form.addInfoMessage("Please justify within comments, the update made to the priority.");
			return false;
		}
	}
}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur, 

Thanks for your response. I have made use of your script above and it works like a charm. From a user experience perspective, could you please suggest if there's a way to make the info message more intuitive? 

For ex - If I've changed the priority from 4-Low to 3-Medium, and the assignment group from GroupABC to GroupXYZ, can we display an info message that says, "The fields : Priority, Assignment Group has changed. Please justify within comments the update made to the record"


Regards,
Akshay

@Akshay Bhaskar 

Thank you for marking my response as helpful.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Akshay Bhaskar 

you can know which field got changed based on that group.Changed flag and based on that show respective messages using multiple if else statements.

I hope you will be able to incorporate those changes.

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader