Dot walk and get the display value of the field

SHALIKAS
Tera Guru

I have two fields on incident table. One is business service which refers to application table and other is medal field which is a string field.

I want that based on the selected application the value of the business criticality should populate on the medal field and if the business criticality value is gold the font color should be red. 

I have written onChange client script on the business service field as follows -

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   
  g_form.getReference('business_service', function(br){
    if(br){
        
        g_form.setValue('u_medal_category',br.busines_criticality);
        var medal=g_form.getControl('u_medal_category');
        if(br.busines_criticality==='gold')
        {
            medal.style.color="red";
        }
        else{
            medal.style.color="";
        }
    }

  });
 }
Using this I am able to populate the value in the medal field and also the color. But I am able to populate the backend value of business criticality in medal field. I want to populate the display value in medal field. I tried using getDisplayValue() but it is returning empty value
1 ACCEPTED SOLUTION

SHALIKAS
Tera Guru
Following is the client script which works for me
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'
});

View solution in original post

3 REPLIES 3

Sandeep Rajput
Tera Patron
Tera Patron

@SHALIKAS In case if 

g_form.setValue('u_medal_category',br.getDisplayValue('busines_criticality'));

isn't working, then I recommend you to create a server side script include and instead of using getReference use GlideAjax to get the display value of busines_criticality field.

 

Hope this helps.

@SHALIKAS That's exactly what I recommended. You can also mark my response an accepted solution.

SHALIKAS
Tera Guru
Following is the client script which works for me
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'
});