Trying to show / hide catalog variable based on a previous value's sys id

servicenowtim
Tera Contributor

I am so lost on this after trying various options and a lot of searching. I'm hoping you all can steer me in the right direction. Went into this change feeling confident, as I have made it work in my PDI. However, in there the CMDB had proper names and defining behavior was more straight forward.

 

Issue is, the CMDB other than prod has no display names. Just shows 'auto' w/ sys ids for the name. I'm not a CMDB expert, assume that's with auto discovery and in prod it adjusts, as dev and test don't show it properly?

Anyways, when I define behavior on my question variable, it's a dead end as there is not value name to assign too it. I can't even use logic I know that works and import it down the line as it won't save the values.

 

End goal: Have a value selected (e.g. 'Google' or 'Yahoo') on a choice record reference (cmdb_ci_service). Grab and confirm the sys id. If it matches, unhides another question variable, makes it mandatory, asking for more info.

 

I tried a script include with Glide AJAX enabled

var VerifySysID = Class.create();
VerifySysID.prototype = {
        initialize: function() {
		},

        VerifySysID: function(sys_id) {
            var result = false;
            var verify = new GlideRecord('cmdb_ci_service');
            gr.addQuery('82b8c5bcc351221029d17fedd401311e', sys_id);
            gr.query();
            if (gr.next()) {
            result = true;
        }
        return result;
    },
    type: 'VerifySysID'
};

And a catalog client script - onChange - UI Type: All - Applies on a Catalog Item View

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

    var glideVerify = new GlideAjax('VerifySysID');
    glideVerify.addParam('sysparm_name', 'VerifySysID');
    glideVerify.addParam('sysparm_sys_id', newValue);
    glideVerify.getXMLAnswer(function(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
                if (answer === 'true') {
            //If sys_id is found, display and make other variables mandatory
            g_form.setDisplay('variable1', true);
            g_form.setMandatory('variable1', true);
        } else {
            //If sys_id is not found, hide and make other variables not mandatory
            g_form.setDisplay('variable1', false);
            g_form.setMandatory('variable1', false);
        }
    });
}

 

Nothing happens when testing it out. Nothing in Script Tracer when running a trace too. Double checked and it appears in the newest version Glide AJAX replaced the client callable option, so I assume that's correct.

1 REPLY 1

servicenowtim
Tera Contributor

Solved. I just used a catalog client script for each variable, with the sys_id baked into it to confirm. After verifying the correct sys_id via the record reference w/ addInfoMessage.

 

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

    //Get sys_id of value in choice record reference
    var choiceValue = g_form.getValue('system'); 
    g_form.addInfoMessage("sys_id - " + choiceValue); //Displays message to show choice sys_id
	
	// Perform verification
	if (choiceValue === '82b8c5bcc351221029d17fedd401311e') {
		// If value matches, show and make the target field mandatory
		g_form.setDisplay('variable1', true);
		g_form.setMandatory('variable1', true);
	} else {
		// If the value does not match, hide and make the target field not mandatory
		g_form.setDisplay('variable1', false);
		g_form.setMandatory('variable1', false);
	}
}