Have to make field visible based on Variable value choice and RITM Value choice, looking for suggest

e__rajesh_badam
Mega Guru

Looking for help i.e., if item = lead manager App and Request type =API requests then make visible of 

Could this late request be fulfilled?
Please suggest.
 
confusion here is Item =  Lead manager App is part of RITM table and Request Type = API Requests variable is part of catalog form. Based on these two value choices want to make field (Could this late request be fulfilled? - Field is part of RITM table) visible,
 
Achieved Above Scenario with below :
 
Script Include:
 
var sendCaseVaribleInfo = Class.create();
sendCaseVaribleInfo.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

sendInfo: function()
{
var CaseNumber = this.getParameter('sysparm_parentSysId');
var gr = new GlideRecord('sc_req_item');
gr.addQuery('sys_id',CaseNumber);
gr.query();
if(gr.next())
{
if(gr.variables.request_type == 'pre-show readiness') // Afte variables.addYourVariableBackEndName and built the logic
{
return "yes";
}
else
{
return "no";
}
}
},
type: 'sendCaseVaribleInfo'
});
 
On-change Client Script: onchange on State value
 
Did written two client script as per my requirement.
 
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        //if (g_form.getValue('state') == '19')
        if ((g_form.getValue('state') == '19') ||((g_form.getValue('state') == '21'))){


            var ga = new GlideAjax('sendCaseVaribleInfo');
            ga.addParam('sysparm_name', 'sendInfo');
            ga.addParam('sysparm_parentSysId', g_form.getUniqueValue());
            ga.getXML(ResponseFunction);

            function ResponseFunction(response) {
                var answer = response.responseXML.documentElement.getAttribute("answer");
                //alert(answer);
                if (answer == 'yes') {
                    g_form.setVisible('u_could_this_late_request_be_fulfilled', true);
                    g_form.setMandatory('u_could_this_late_request_be_fulfilled', true);

                } else {
                    g_form.setVisible('u_could_this_late_request_be_fulfilled', false);
                    g_form.setMandatory('u_could_this_late_request_be_fulfilled', false);
                }
            }
        }
        else{
            g_form.setVisible('u_could_this_late_request_be_fulfilled', false);
                    g_form.setMandatory('u_could_this_late_request_be_fulfilled', false);
        }
    }


    //Type appropriate comment here, and begin script below
   // if (g_form.getValue('state') == '19')
    if ((g_form.getValue('state') == '19') ||((g_form.getValue('state') == '21'))){


        var ga = new GlideAjax('sendCaseVaribleInfo');
        ga.addParam('sysparm_name', 'sendInfo');
        ga.addParam('sysparm_parentSysId', g_form.getUniqueValue());
        ga.getXML(ResponseFunction);

        function ResponseFunction(response) {
            var answer = response.responseXML.documentElement.getAttribute("answer");
            //alert(answer);
            if (answer == 'yes') {
                g_form.setVisible('u_could_this_late_request_be_fulfilled', true);
                g_form.setMandatory('u_could_this_late_request_be_fulfilled', true);
            } else {
                g_form.setVisible('u_could_this_late_request_be_fulfilled', false);
                g_form.setMandatory('u_could_this_late_request_be_fulfilled', false);
            }
        }
    }
    else{
            g_form.setVisible('u_could_this_late_request_be_fulfilled', false);
                    g_form.setMandatory('u_could_this_late_request_be_fulfilled', false);
        }
}
 
On-Load Client Script:
 

 

function onLoad() {
   // if (g_form.getValue('state') == '19')
    if ((g_form.getValue('state') == '19') ||((g_form.getValue('state') == '21'))){


        var ga = new GlideAjax('sendCaseVaribleInfo');
        ga.addParam('sysparm_name', 'sendInfo');
        ga.addParam('sysparm_parentSysId', g_form.getUniqueValue());
        ga.getXML(ResponseFunction);

        function ResponseFunction(response) {
            var answer = response.responseXML.documentElement.getAttribute("answer");
          //  alert(answer);
            if (answer == 'yes') {
                g_form.setVisible('u_could_this_late_request_be_fulfilled', true);
                g_form.setMandatory('u_could_this_late_request_be_fulfilled', true);
            } else {
                g_form.setVisible('u_could_this_late_request_be_fulfilled', false);
                g_form.setMandatory('u_could_this_late_request_be_fulfilled', false);
            }
        }
    }
    else{
            g_form.setVisible('u_could_this_late_request_be_fulfilled', false);
                    g_form.setMandatory('u_could_this_late_request_be_fulfilled', false);
        }
}

 

7 REPLIES 7

Brad Bowman
Kilo Patron
Kilo Patron

Using a Client Script (not Catalog) on the sc_req_item table, g_form has access to the fields on the RITM record and variables from the Catalog Item, so you can use it just the same with the field/variable name and no other distinction.

e__rajesh_badam
Mega Guru

Hi @Brad Bowman  ,

 

Tried already with onload client script on sc_req_item table with below script but it is not working.

Could you please help me out it.

-----------------------------------------------------------------------------------------------------

function onLoad() {

    //Type appropriate comment here, and begin script below
    if ((g_form.cat_item == 'RX Lead Manager App (Emperia)') && (g_form.state == '19') && (g_form.variable.request_type == '495365a21be021108f7111f8bc4bcbbd'))

    {
        g_form.setVisible('u_could_this_late_request_be_fulfilled', true);

    }

}
-------------------------------------------------------------------------------------------------------------------

It would look more like this to follow the syntax of g_form

function onLoad() {

     if (g_form.getValue('cat_item') == '<<sys_id of catalog item>>' && g_form.getValue('state') == '19' && g_form.getValue('request_type') == '495365a21be021108f7111f8bc4bcbbd') {
        g_form.setVisible('u_could_this_late_request_be_fulfilled', true);
    }
}

If the field/variable is Visible by default, then you would need an else to setVisible false.

 

https://docs.servicenow.com/bundle/xanadu-api-reference/page/app-store/dev_portal/API_reference/Glid... 

e__rajesh_badam
Mega Guru

Hi @Brad Bowman  ,

request_type is variable which is part of catalog form. So when we called it wont work i guess.

 

Reason for saying is when trying the script which you shared it is not working. Also, i given Variable choice value is name instead of sys_id. Even this scenario also variable is

Could this late request be fulfilled? is displaying

.

 

function onLoad() {

    if ((g_form.getValue('cat_item') == 'f3fff70ddb2ac8107030ed384b9619dc') && (g_form.getValue('state') == '19') && (g_form.getValue('request_type') == 'pre-show readiness'))

    {
        g_form.setVisible('u_could_this_late_request_be_fulfilled', false);
     
    }
}