Call UI Page from UI Action

Ankita Gupte
Kilo Sage

Hello Experts,

 

I have scoped application Recipe Management, in the same scoped application I have created custom table recipe management histories which will create the line item in table if cost price is update for any recipe tagging the recipe name and updated date/time.

Below is my UI Page code which open the attached window when I use try it option on UI Page. But when I try to call same from UI action then UI action does not work.

 

UI Page code:

Name: view_cost_dialog

Code:

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

  <!-- Read parameters passed from UI Action -->
  <g:evaluate>
    var recipeSysId = input.getPreference('recipeSysId');
    var effectiveMonthPref = input.getPreference('effectiveMonth');
  </g:evaluate>

  <!-- Query distinct recipes from history table -->
  <g2:script> redesign script using GlideModal API
    var recipeList = [];
    var gr = new GlideRecord('x_pptap_recipe_m_0_recipe_management_history');
    gr.query();
    while (gr.next()) {
      var recipeId = gr.getValue('recipe');
      var recipeName = gr.getDisplayValue('recipe');
      // Avoid duplicates
      if (!recipeList.some(function(r){ return r.sys_id === recipeId;})) {
        recipeList.push({sys_id: recipeId, name: recipeName});
      }
    }
  </g2:script>

  <table style="margin:20px 10px;">
    <tr>
      <td><label for="recipe_field">Recipe</label></td>
      <td>
        <select id="recipe_field">
          <option value="">Select a Recipe</option>
          <j2:forEach var="r" items="${recipeList}">
            <j:choose>
              <j:when test="${r.sys_id == recipeSysId}">
                <option value="${r.sys_id}" selected="selected">${r.name}</option>
              </j:when>
              <j:otherwise>
                <option value="${r.sys_id}">${r.name}</option>
              </j:otherwise>
            </j:choose>
          </j2:forEach>
        </select>
      </td>
    </tr>

    <tr>
      <td><label for="month_field">Effective Month</label></td>
      <td>
        <input type="month" id="month_field" value="${effectiveMonthPref || ''}"/><br/>
      </td>
    </tr>

    <tr>
      <td><label for="cost_result">Cost Price</label></td>
      <td>
        <input type="text" id="cost_result" readonly="true"/><br/>
      </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="getCost()">Get Cost</button>
      </td>
    </tr>
  </table>

  <script>
    function getCost() {
      var recipeId = document.getElementById("recipe_field").value;
      var month = document.getElementById("month_field").value;

      if (!recipeId || !month) {
        alert("Please select both recipe and effective month.");
        return;
      }

      var ga = new GlideAjax('GetRecipeCostAjax');
      ga.addParam('sysparm_name', 'getCost');
      ga.addParam('sysparm_recipe', recipeId);
      ga.addParam('sysparm_month', month);
      ga.getXMLAnswer(function(response) {
        document.getElementById("cost_result").value = response || "Not found";
      });
    }

    function closeWindow() {
      GlideDialogWindow.get().destroy();
    }
  </script>

</j:jelly>
 
AnkitaGupte_0-1753166141454.png

UI Action:

Name: View Cost

Action Name: view_cost

AnkitaGupte_1-1753166186080.png

 

Code:

function onClick() {
   
  var dialog = new GlideDialogWindow('view_cost_dialog'); // no ".do"
  dialog.setTitle('View Cost');
  dialog.setPreference('recipeSysId', g_form.getUniqueValue());

  // Optional: add current month
  var today = new Date();
  var month = today.toISOString().slice(0, 7); // format yyyy-MM
  dialog.setPreference('effectiveMonth', month);

  dialog.render();
}
 
The UI action appears on form but when clicked its not working.
and in UI page I am trying to load recipes names as well but it is not giving me recipes name in drop down.
 
Can you please help me to fix this.
 
On form level when click on view cost if recipe name can be autopoulated from form recipe will be better then only based on date that month cost price can be shown.
3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

@Ankita Gupte 

when you call that UI page give the complete name including the scope prefix etc but don't include .do

var dialog = new GlideDialogWindow('view_cost_dialog'); // no ".do"

AnkurBawiskar_0-1753167440160.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Hi Ankur,

 

Thank you for the response.

 

As suggested I added below end point url in UI action as below but still UI action is not working

AnkitaGupte_0-1753170854596.png

UI Action code below:

AnkitaGupte_1-1753170904077.png

 

 

 

@Ankita Gupte 

then some error coming in UI page

Try checking the browser console error.

If my response helped please mark it correct and close the thread so that it benefits future readers.

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