Styling works in onChange but not visible after submitting the form

SHALIKAS
Tera Guru

I have a onChange client script written to change the color of a medal category field based on the application selected in incident table for a field. The changing of color works if I change the value of the application field. But when I submit the form the color is not displayed

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading) {
        return;
    }
    if (!newValue || newValue == '') {
        g_form.setValue('u_medal_category', '');
        return;
    }

    var ga = new GlideAjax('highlight_medal_category');
    ga.addParam('sysparm_name', "getUserInfo");
    ga.addParam('sysparm_ci', newValue);
    ga.getXMLAnswer(function(answer) {
        var response = JSON.parse(answer);
        g_form.setValue('u_medal_category', response.bus);
        var medal = g_form.getControl('u_medal_category');
       
       
        if (response.bus == '1 - Gold') {
            medal.style.color = "red";
           
           
       
        } else if (response.bus == '2 - Silver') {
            medal.style.color = "orange";
        } else if (response.bus == '3 - Bronze') {
            medal.style.color = "green";
        } else {
            medal.style.color = "";
        }
       
    });
}
Script Include
var highlight_medal_category = Class.create();
highlight_medal_category.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getUserInfo: function() {
        var service_ci = this.getParameter('sysparm_ci');
        var gr = new GlideRecord("cmdb_ci_service");
        gr.get(service_ci);
        var bus = gr.busines_criticality.getDisplayValue();
        var response = {};
        response.bus = bus;
        return JSON.stringify(response);

    },

    type: 'highlight_medal_category'
});
1 ACCEPTED SOLUTION

SHALIKAS
Tera Guru

Hi, I finally found the solution

After saving the form it is reloaded, and this client scripts doesn't run onload, that is why it is not executing.

We need a script that runs onchange as well as onload.

We can either remove the isloading check form this script then it will work. But it wil be highlighted in healthscans as going against best practice.

Or we can create another Onload script that does the same thing.

View solution in original post

1 REPLY 1

SHALIKAS
Tera Guru

Hi, I finally found the solution

After saving the form it is reloaded, and this client scripts doesn't run onload, that is why it is not executing.

We need a script that runs onchange as well as onload.

We can either remove the isloading check form this script then it will work. But it wil be highlighted in healthscans as going against best practice.

Or we can create another Onload script that does the same thing.