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 can we define Dynamic Reference Qualifier in HTML Body - UI Page

Irfan Khan Path
Tera Contributor

In UI Page scripting, Can we take the values from "Client Script" and define that as a Dynamic Reference Qualifier in the "HTML" body?

As per my requirement: I have to show Stockrooms based on the "requested_for" country
So this client script is successfully fetching the Country details of the "requested_for" user.

(function () {
    var selectedUser = g_form.getValue('requested_for');
    alert(selectedUser);
    var ajaxexample = new GlideAjax('CountryUtils');
    ajaxexample.addParam('sysparm_name', 'getCountry');
    ajaxexample.addParam('sysparm_user', selectedUser);
    ajaxexample.getXML(getResponse);
    g_form.addInfoMessage("This is Ok1" + selectedUser);

    function getResponse(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        //alert(answer);
        g_form.addInfoMessage("Test 1" + answer);
   
    }
})();

So, we are storing the Country of the requested for in 'answer'.

But, when coming to HTML 
 
        <tr>
            <td style="text-align:right;"><label style="color:red">*</label><label id="stockroom_label" title="${gs.getMessage('Stockroom to which the loaner asset is returned')}">${gs.getMessage('Stockroom:')}</label></td>
            <td id="td_reference_stockroom"><g:ui_reference name="stockroom" id="stockroomId" table="alm_stockroom" aria-labelledby="stockroom_label" query= "location.u_location_country=2450e7eddbff764005ea766eaf9619ec/></td>
        </tr>

In query, I know how to add the static filter, but in the place of sys_id, I should pass the whatever value that comes in 'answer' in the given client script. So that it will become a dynamic filter condition. 


Based upon the requested_for --> Country, we need to display the stockrooms
 
Please help me to achieve this requirement.
 
Thanks in advance!!!
2 REPLIES 2

Community Alums
Not applicable

Hi @Irfan Khan Path ,

 

The issues i can see is- <g:ui_reference> tag is hardcoded and doesnt update based on the client script’s answer value.

One more issue - The reference qualifier is defined in the HTML body before the client script fetches the value

 

Please find the updated scripts-

Client Script-

(function () {
    var selectedUser = g_form.getValue('requested_for');
    var ajaxexample = new GlideAjax('CountryUtils');
    ajaxexample.addParam('sysparm_name', 'getCountry');
    ajaxexample.addParam('sysparm_user', selectedUser);
    ajaxexample.getXMLAnswer(function(response) {
        var answer = response;
        g_form.addInfoMessage("Country: " + answer);
        // update the reference qualifier dynamically
        updateStockroomReferenceQualifier(answer);
    });
})();

function updateStockroomReferenceQualifier(country) {
    var stockroomField = g_form.getControl('stockroom');
    stockroomField.setAttribute('data-ref-qual', 'location.u_location_country=' + country);
    g_form.clearOptions('stockroom');
    g_form.addOption('stockroom', '', '-- None --');
}

 

HTML-

<tr>
    <td style="text-align:right;">
        <label style="color:red">*</label>
        <label id="stockroom_label" title="${gs.getMessage('Stockroom to which the loaner asset is returned')}">
            ${gs.getMessage('Stockroom:')}
        </label>
    </td>
    <td id="td_reference_stockroom">
        <g:ui_reference name="stockroom" id="stockroomId" table="alm_stockroom" aria-labelledby="stockroom_label"/>
    </td>
</tr>

 

If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!

 

Thanks & Regards,

Sanjay Kumar

Irfan Khan Path
Tera Contributor

Hello Sanjay, 

Thanks for your reply.

But its not working. I have to use the Dynamic filter in HTML by taking the Country Value from Client Script. Is that Possible. 

As per your code, I'm getting the sys_id of that 'requested_for". 
With that country, we have to add it in the place of below sys_id, so that it will apply the filter dynamically, Is that possible?
query= "location.u_location_country=2450e7eddbff764005ea766eaf9619ec/>

Thanks...