Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Jelly script not working

praveeng2
Tera Contributor

Hi Guys, I am trying to hide one of the macro 'add_location' present beside assignment group field when the case state is resolved or closed complete. However I am not able to get this worked. Please suggest. Thanks

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<j:set var="jvar_num" value="assign_to_user_${ref}"/>
<g:evaluate var=caseState">
current.getValue('state');
</g:evaluate>
<a id="${jvar_num}" onclick="addLocation('${ref}')">
<img border="0" title="${gs.getMessage("Add Location")}"
</a>
<script>
function Onchange_state(element,original, changed, loading){
var visibility = 'hidden';
if(changed.state == '3' || changed.state =='150') { // case state
e.style.visibility = visibility;
} else {

e.style.visibility = 'visible';
}
}
</script>
</j:jelly>
3 REPLIES 3

Siddhesh Jadhav
Kilo Sage

Hi @praveeng2,

Here’s the corrected answer in two clear points, followed by a working script:

 

1. What was wrong

  • Tag issues:
    • <g:evaluate var=caseState"> is malformed; it should be var="caseState".
    • The <img> tag is incomplete (missing src and closing />).
  • JavaScript issues:
    • Variable e is undefined in your function; you need to target the actual element by ID.
    • The function parameters don’t match ServiceNow’s onChange convention (newValue, oldValue, isLoading).

2. Correct script

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide">

    <!-- Create a dynamic ID for the link -->
    <j:set var="jvar_num" value="assign_to_user_${ref}"/>

    <!-- Evaluate the current state -->
    <g:evaluate var="caseState">
        current.getValue('state');
    </g:evaluate>

    <!-- Add Location link -->
    <a id="${jvar_num}" onclick="addLocation('${ref}')">
        <img src="images/icons/add.png" border="0" 
             title="${gs.getMessage('Add Location')}" />
    </a>

    <script>
        function Onchange_state(newValue, oldValue, isLoading) {
            var link = document.getElementById('${jvar_num}');
            if (!link) return;

            if (newValue == '3' || newValue == '150') {
                link.style.visibility = 'hidden';
            } else {
                link.style.visibility = 'visible';
            }
        }
    </script>
</j:jelly>

This corrected script ensures:

  • Proper Jelly syntax.
  • A complete <img> tag.
  • The visibility logic correctly targets the anchor element by ID.

 

Thanks & Regards,
Siddhesh Jadhav

 

Accept and mark helpful if it resolved your query.

Ankur Bawiskar
Tera Patron

@praveeng2 

see this link has working solution, check and enhance for your requirement

this link has working solution'

Hide UI Macro 

Hiding a UI Macro based on Field Data 

response from ewok

AnkurBawiskar_0-1766051837180.png

by following 1st link, I updated your script, try that

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
    <j:set var="jvar_num" value="assign_to_user_${ref}" />
    <g:evaluate var="caseState" jelly="true">
        var state = current.getValue('state');
        state;
    </g:evaluate>
    <j:if test="${caseState != '3' &amp;&amp; caseState != '150'}">
        <a id="${jvar_num}" onclick="addLocation('${ref}')">
            <img border="0" title="${gs.getMessage(' Add Location')}"></img></a>
    </j:if>
</j:jelly>

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

@praveeng2 

Hope you are doing good.

Did my reply answer your question?

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader