Duplicate Info Message on Workspace Form (OnChange Client Script)

ujjwala_678
Tera Contributor

Hi everyone,

We recently upgraded our instance to the Washington DC release and noticed an issue with an OnChange client script in Workspace. When the field value changes, the script displays duplicate info messages. However, the same script works fine in Native UI without any duplicates.

Has anyone else encountered this issue? Any insights or suggestions on the possible cause and resolution would be greatly appreciated.

Thank you!

 

 

Onchange Client Script
UI Type : All
Field Name: safety_impact_scale
Code:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading || newValue === '') {
		return;
	}

	showInfoMessage();


	function showInfoMessage() {

		if (newValue == '1') {
			g_form.showFieldMsg('safety_impact_scale', "Multiple Fatalities", 'info');
		} else if (newValue == '2') {
			g_form.showFieldMsg('safety_impact_scale', "One or more Fatalities ", 'info');
		} else if (newValue == '3') {
			g_form.showFieldMsg('safety_impact_scale', "SM/Serious injury", 'info');
		} else if (newValue == '4') {
			g_form.showFieldMsg('safety_impact_scale', "Medical treatment", 'info');
		} else if (newValue == '5') {
			g_form.showFieldMsg('safety_impact_scale', "Minor injury", 'info');
		}
		else if (newValue == '6') {
            g_form.showFieldMsg('safety_impact_scale', "N/A", 'info');
        }
		

	}

}

 

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@ujjwala_678 

before upgrade everything was working fine in workspace as well?

Are you sure no change happened in that script after the upgrade?

can you try this and see if it works in both places?

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    // Clear any existing messages
    g_form.hideFieldMsg('safety_impact_scale');

    showInfoMessage();

    function showInfoMessage() {
        if (newValue == '1') {
            g_form.showFieldMsg('safety_impact_scale', "Multiple Fatalities", 'info');
        } else if (newValue == '2') {
            g_form.showFieldMsg('safety_impact_scale', "One or more Fatalities or permanent disabilities/Non Consensual S.Pen. or multiple attempted non-consensual SA", 'info');
        } else if (newValue == '3') {
            g_form.showFieldMsg('safety_impact_scale', "SM/Serious Physical Assault/other serious injury", 'info');
        } else if (newValue == '4') {
            g_form.showFieldMsg('safety_impact_scale', "Medical treatment", 'info');
        } else if (newValue == '5') {
            g_form.showFieldMsg('safety_impact_scale', "Minor injury", 'info');
        } else if (newValue == '6') {
            g_form.showFieldMsg('safety_impact_scale', "N/A", 'info');
        }
    }
}

 

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

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

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@ujjwala_678 

before upgrade everything was working fine in workspace as well?

Are you sure no change happened in that script after the upgrade?

can you try this and see if it works in both places?

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    // Clear any existing messages
    g_form.hideFieldMsg('safety_impact_scale');

    showInfoMessage();

    function showInfoMessage() {
        if (newValue == '1') {
            g_form.showFieldMsg('safety_impact_scale', "Multiple Fatalities", 'info');
        } else if (newValue == '2') {
            g_form.showFieldMsg('safety_impact_scale', "One or more Fatalities or permanent disabilities/Non Consensual S.Pen. or multiple attempted non-consensual SA", 'info');
        } else if (newValue == '3') {
            g_form.showFieldMsg('safety_impact_scale', "SM/Serious Physical Assault/other serious injury", 'info');
        } else if (newValue == '4') {
            g_form.showFieldMsg('safety_impact_scale', "Medical treatment", 'info');
        } else if (newValue == '5') {
            g_form.showFieldMsg('safety_impact_scale', "Minor injury", 'info');
        } else if (newValue == '6') {
            g_form.showFieldMsg('safety_impact_scale', "N/A", 'info');
        }
    }
}

 

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

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

Hi @Ankur Bawiskar ,

This issue began occurring only after the upgrade. However, after implementing the changes you suggested in my client script, the duplicate message was successfully removed—it worked like a charm!

Thank you so much! ðŸ¤—

Tushar
Kilo Sage
Kilo Sage

Hi @ujjwala_678 - can you please try below code -

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    // Clear previous messages
    g_form.clearMessages();

    // Show the appropriate message
    showInfoMessage();

    function showInfoMessage() {
        var message = "";

        switch (newValue) {
            case '1':
                message = "Multiple Fatalities";
                break;
            case '2':
                message = "One or more Fatalities or permanent disabilities/Non Consensual S.Pen. or multiple attempted non-consensual SA";
                break;
            case '3':
                message = "SM/Serious Physical Assault/other serious injury";
                break;
            case '4':
                message = "Medical treatment";
                break;
            case '5':
                message = "Minor injury";
                break;
            case '6':
                message = "N/A";
                break;
        }

        if (message) {
            g_form.showFieldMsg('safety_impact_scale', message, 'info');
        }
    }
}

 

If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful." Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Medi C
Giga Sage

Hi @ujjwala_678 ,

 

Please adjust your function by clearing the field message first.

 

 

 

function showInfoMessage() {
                g_form.hideFieldMsg('safety_impact_scale');
		if (newValue == '1') {
			g_form.showFieldMsg('safety_impact_scale', "Multiple Fatalities", 'info');
		} else if (newValue == '2') {
			g_form.showFieldMsg('safety_impact_scale', "One or more Fatalities ", 'info');
		} else if (newValue == '3') {
			g_form.showFieldMsg('safety_impact_scale', "SM/Serious injury", 'info');
		} else if (newValue == '4') {
			g_form.showFieldMsg('safety_impact_scale', "Medical treatment", 'info');
		} else if (newValue == '5') {
			g_form.showFieldMsg('safety_impact_scale', "Minor injury", 'info');
		}
		else if (newValue == '6') {
            g_form.showFieldMsg('safety_impact_scale', "N/A", 'info');
        }
		

	}

 

 

You could also user Switch Case instead of multiple if-else statments:

 

 

function showInfoMessage() {
    g_form.hideFieldMsg('safety_impact_scale'); // Clears previous messages before adding a new one

    var msg = '';
    switch (newValue) {
        case '1':
            msg = "Multiple Fatalities";
            break;
        case '2':
            msg = "One or more Fatalities";
            break;
        case '3':
            msg = "SM/Serious injury";
            break;
        case '4':
            msg = "Medical treatment";
            break;
        case '5':
            msg = "Minor injury";
            break;
        case '6':
            msg = "N/A";
            break;
    }

    if (msg) {
        g_form.showFieldMsg('safety_impact_scale', msg, 'info');
    }
}

 

 




If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.