UI Macro - How to get the value of a reference field in jelly?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2023 06:29 AM
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?
Thank you in advance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2023 06:58 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2023 07:19 AM
I dont have the incident created yet.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2023 07:05 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2023 08:49 AM
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