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.

How to call dynamic query in the UI Page refernce field

Dinesh9
Giga Contributor

Hi Everyone,

I have been creating an ui page where I am trying to select  a change related to an incident and I am setting that change value in the incident form's caused by field.

-How can I pass the dynamic query in the ui page ?

-How to get the sys_id in a client script without using get and setPreference methods?

- How can I redirect it a same current incident page ?

 

URL:

(url = 'active=true^stateIN0,3^end_dateBETWEENjavascript:gs.daysAgo(14)@javascript:gs.endOfToday()^ORwork_endBETWEENjavascript:gs.daysAgo(14)@javascript:gs.endOfToday()' +
'^cmdb_ci=' + g_form.getValue('business_service') + '^ORcmdb_ci=' + g_form.getValue('cmdb_ci'))

 

My code: HTML

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

//my code
<g:ui_form>

 <input type="hidden" id="system_id" name="system_id" value="${sys_id}"/> 
<tr>
<td style ="width:25%">
<g:form_label>
Related Change Requests :
</g:form_label>
<td style ="width:60%">
<g:ui_reference name= "chg" id="chg" query="active=true^end_dateBETWEENjavascript:gs.beginningOfLast7Days()@javascript:gs.beginningOfLast2Hours()" table="change_request"/>   ---> // the query i want to replace with a dynamic query

</td>
</td>
</tr>
<tr>
<td colspan="2" style="text-align:right;padding-top:10px;">
<button class="btn btn-default" onclick="closeWindow()" style="margin-right:10px;">Cancel</button>
<button class="btn btn-primary" onclick="update_ticket()">Ok</button>
</td>

</tr>

</g:ui_form
</j:jelly>

Client script in UI page:

function update_ticket() {
var gdw = new GlideDialogWindow.get();
var rel_chg = gel('chg').value;
var sys_id = gdw.getPreference('sys_id'); // is there any other way to get the sys_id
var inc = new GlideRecord('incident');
inc.addQuery('sys_id', sys_id);
inc.query();
if (inc.next()) {
inc.setValue('caused_by', rel_chg);
inc.update();
GlideDialogWindow.get().destroy();

}

Processing script in UI Page

var url = 'incident.do?sys_id=' + system_id;
response.sendRedirect(url); ----> redirecting to an incident page where the sys id is empty

 

Client Script in IT incident form:

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

//Type appropriate comment here, and begin script below
var dia = new GlideDialogWindow('inc_chg');
dia.setTitle('Related IT Changeee');
dia.setSize(650,650);
dia.setPreference('sys_id', g_form.getUniqueValue());
dia.render();

}

 

Thanks in advance,

Dinesh

5 REPLIES 5

Periyasamy P
Tera Guru

Dynamic filter, you can write logic and return filter which you want to use

<g:evaluate var="jvar_filter">
 var filter = "number=VUL0001083";
 filter;
</g:evaluate>
	

<g:ui_reference name="vuln_group" id="vuln_group" table="sn_vul_vulnerability" query="${jvar_filter} />

 

You can store the sys_id in hidden input and you can use it your scripts,

<input type="hidden" class="recSysID" id="recSysID" name="recSysID" value="${sysparm_sys_id}"></input>	 

 

Redirect, 

response.sendRedirect("<table>.do?sysparm_query=sys_id=" + recSysID);

Hi Periyasamy,

I have changed as you mentioned but its not working. can you please check at the above code

- Instead of getting the incident record's sys_id , I am getting the UI page's sysid 

Thanks,
Dinesh

In this script, ${sysparm_sys_id} should be passed from client script from where we are calling this UI page. For ex, dia.setPreference('sysparm_sys_id', g_form.getUniqueValue());

 

To access value,

Client script: gel('recSysID').value 

In processing script: var sysID = recSysID;

 

 

<input type="hidden" class="recSysID" id="recSysID" name="recSysID" value="${sysparm_sys_id}"></input>	

Hi Periyasamy ,

Thanks it is working. I am able to get the sysid of the current record now. I have one more query.

How can i pass the dynamic query which I mentioned below in my code?

I want my reference field to show only the records which satisfies the below query

url = 'active=true^stateIN0,3^end_dateBETWEENjavascript:gs.daysAgo(14)@javascript:gs.endOfToday()^ORwork_endBETWEENjavascript:gs.daysAgo(14)@javascript:gs.endOfToday()' +
'^cmdb_ci=' + g_form.getValue('business_service') + '^ORcmdb_ci=' + g_form.getValue('cmdb_ci')

Regards,

Dinesh