The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Amol4
Tera Contributor

This article is about showing how one can add a field to Lookup Select Box.

By default the lookup select box references a table to populate values.

i came across a case where I needed to display values on RITM form but not on Portal. This was a tricky problem as the reference field/lookup select box calculates its field values on server side. Either you can remove it from both places or keep it but you cannot do it at the single place.

After much research and playing around I found this solution.

First get control of the element i.e. lookup select box using the below code

var form = typeof g_sc_form != "undefined" ? g_sc_form : g_form;
var myControl = form.getControl("change_type");

 Then we can use the native code for creating Option and add it to Choice list that gets created from Select Box. 

var opt = new Option(<Display Value of the reference record>, <Sys ID of the reference record>);
opt.setAttribute("style", "color:red");    // Colors the choice
opt.setAttribute('disabled','true');       // Disables selecting
myControl.options.add(opt);                // Add it to the Select Element

PS: This only works on RITM view and not on Service Portal.

 

This is how I used it along with GlideAjax in Client Script.

function onLoad() {
    //Type appropriate comment here, and begin script below
    var ga = new GlideAjax('clientMethodUtils');
    ga.addParam('sysparm_name', 'getArchivedChangeType');
    ga.getXML(Callback);

    function Callback(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        var returneddata = JSON.parse(answer);

        SetArchivedChangeType(returneddata);
    }

    function SetArchivedChangeType(returneddata) {
        var form = typeof g_sc_form != "undefined" ? g_sc_form : g_form;
        var myControl = form.getControl("change_type");

        for (var i = 0; i < returneddata.length; i++) {
            var opt = new Option(returneddata[i].change_type.toString(), returneddata[i].sys_id.toString());
            opt.setAttribute("style", "color:red");
            //opt.setAttribute('disabled','true');
            myControl.options.add(opt);
        }
    }
}
Comments
JingRenT
Tera Contributor

Does not work.

Version history
Last update:
‎08-03-2020 02:18 AM
Updated by: