UI Macro - How to get the value of a reference field in jelly?

JLeong
Mega Sage

Good day!

 

In the incident form, I have UI macro beside the caller name. The UI macro serves as an identifier to show if the caller is a member of a specific group. When hovering the mouse over the icon, I need to show the group name.

 

When populating the incident form, how do I get the value of the caller field in jelly? 

 

<g2:evaluate var="jvar_project_name" jelly="true">
//Get the special project name, if it exsists, to display on hover of the icon
var name = "";
var userObject = g_form.getReference('caller_id');
var empID = userObject.employee_number;
gs.log("ID =" + empID,');
....
</g2:evaluate>

 

Thank you in advance.

6 REPLIES 6

Shivam28
Tera Contributor

Hi,

 

You could use below script inside the Jelly scripting:

<script>

function getCallerDetails()

{ var incidentSysId = g_form.getUniqueValue(); // get current incident record sys_id var ga = new GlideAjax('GetCallerDetails'); // create a new GlideAjax object

ga.addParam('sysparm_name', 'getCallerName'); // specify the server-side function to call ga.addParam('sysparm_incident', incidentSysId); // pass the current incident record sys_id as a parameter ga.getXML(onSuccess, onFailure); // make the asynchronous call to the server

}

 

function onSuccess(response) {

var callerName = response.responseXML.documentElement.getAttribute('caller_name'); // get the caller name from the server response

alert('Caller Name: ' + callerName);

}

 

function onFailure(response) {

alert('Error fetching caller details: ' + response.responseText);

}

</script>


*You need to query the incidentSysId and fetch the callerName in the script include.

I dont have the incident created yet.

-O-
Kilo Patron
Kilo Patron

You are mixing code that generates a web page with code that has been generated. With UI Macros (as part of UI Pages) what happens is that:

1. the browser requests a page (e.g. /incident.do?sys_id=ab12....)

2. a processor intercepts the request and

3. using Jelly generates the page

4. the generated page is sent to the browser

5. the browser displays the received page and runs any script contained in that page.

6. the user modifies the displayed page, that includes a form element and submits the page/form - which is again a request basically

7. a processor intercepts that request and the cycle is executed once more.

 

j:*, j2:, g:* and g2:* tags in UI Pages and UI Macros are evaluated in step 3. - the result and output of those evaluations ends up the source of the web page. These are evaluated nodes. One can also include plain HTML nodes into a Jelly file/UI Macro, but hose are simply output by Jelly, are not interpreted, like <p/> or <script/>.

 

g_form becomes a thing only in step 5.

 

So you can now see how the example shown above cannot possibly work.

 

Put differently Jelly is server side technology/code, g_form is client (browser) side technology code - executed in totally different environment at totally different times. Think of Jelly and its tags as PHP and its tag.

 

So you cannot possibly "get" the value of anything on a web page in a g2: Jelly node, because when Jelly runs, there is no web page yet, for sure it hasn't even gotten to the the browser yet.

 

What you are trying to do, get the value of a field of a form, assuming your UI Macro is part of a UI Page that is displayed "over" a form, should be done in a <script/> tag. But, of course, there is no gs.log client side, you would need to use console.log and thus the result would show up not in ServiceNow's logs, but in the browser's console.

Amit Gujarathi
Giga Sage
Giga Sage

Hi @JLeong ,
I trust you are doing great.
To get the value of the caller field in Jelly, you can use the following code snippet:

 

<g2:evaluate var="jvar_caller_id" jelly="true">
  var callerID = g_form.getValue('caller_id');
</g2:evaluate>

 

This code will fetch the value of the caller field and store it in the jvar_caller_id variable.

Now, to display the group name on hover of the UI macro, you can use the following code:

 

<g2:evaluate var="jvar_group_name" jelly="true">
  var callerID = g_form.getValue('caller_id');
  var callerGroup = "";
  
  if(callerID){
    var userGR = new GlideRecord('sys_user');
    userGR.get(callerID);
    var groupGR = new GlideRecord('sys_user_grmember');
    groupGR.addQuery('user', callerID);
    groupGR.query();
    
    while(groupGR.next()){
      callerGroup += groupGR.getValue('group') + "; ";
    }
  }
  
  callerGroup = callerGroup.replace(/;\s*$/, "");
  
  if(callerGroup){
    gs.log("Caller Group(s): " + callerGroup);
    var groupIcon = '<img src="/sys_user_group.do?sys_id=' + groupGR.getValue('group') + '" alt="' + callerGroup + '" title="' + callerGroup + '" />';
    gs.print(groupIcon);
  }
</g2:evaluate>

 


Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi