Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Issue with Accessing Variable Value in Variable Set

Lisa Goldman
Kilo Sage

Hello,

I need help retrieving the value from a Variable Set.

Here's what I’ve tried so far, but it’s not triggering the alert. Thank you

 

LisaGoldman_0-1752767284855.png

 

 

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

    //Type appropriate comment here, and begin script below

    var applicationName = g_form.getDisplayValue('business_application');

    if (applicationName === 'Maximo') {
        alert(applicationName);
    }

}

   

28 REPLIES 28

@John Gilmore 

Thank you so much for the detailed explanation. As I mentioned earlier in the thread, hardcoding the sys_id is working, and the code I’m using is similar to what you suggested.

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return; // Prevents script from running during form load or if no value is selected
    }

    alert(newValue);

    if (newValue === 'bc29e258db2f9c1005fddbd4ce96195b') { //Maximo
        g_form.setDisplay('service_type', false);

    } else {
		g_form.setDisplay('service_type', true);
	}
}

 

However, I’m still wondering if there’s a way to retrieve the actual display name instead of the sys_id.

 

 

I haven't done it but you might be able to use the getDisplayValue() method in combination with the getReference...try something like this for the if statement:

g_form.getReference('business_application', function(ref) {
    if (ref && ref.getDisplayValue() === 'Maximo') { //This is checking that getReference returned a valid record and also your display value at the same time.
        alert('Maximo');
    }
});

 

This could represent a much easier way to cover multiple possible business_application selection if they all share a single display value.

Edit: If this works then it would present a much easier way to perform this type of check that wouldn't break if the display value field were changed or the referenced record sys_id was different between instances or changed.

@John Gilmore 

After trying your suggested code, the alert is not triggering at all.  Thank you

When you posted this reply I looked at the code and there was an error if you copied exactly. It started with g-form instead of g_form...

I corrected it in the above post, if you copied it exactly try it after fixing that little typo.

@John Gilmore 

I noticed there was a syntax error in the code, so I corrected it and tested again — but the alert still didn’t fire. Again, thank you so much for your time and effort.  Will hardcode the sys_id to make it works.

 

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

    g_form.getReference('busines_application', function(ref) {
        if (ref && ref.getDisplayValue() === 'Maximo') { //This is checking that getReference returned a valid record and also your display value at the same time.
            alert('Maximo');
        }
    });
}