UI Action with GlideDialogWindow

aprilz
Giga Contributor

Is it possible to start with a UI Action on the sc_task form, use the GlideDialogWindow to gather information and save that information to the parent RITM? I have used the examples for creating a GlideDialogWindow so I have the popup box to collect the information but not sure how to write it to the parent RITM. I know the client script on the UI page isn't correct when I say g_form.setValue because I'm on the wrong form since I'm on the sc_task. Is there a way to redirect to the parent RITM or just update the RITM from the sc_task form?

I am hoping to give our fulfillers a way to cancel a request from the task if need be while still gathering the same information as if they were cancelling it from the RITM for reporting purposes.

UI Action script

function commentsDialog() {

    //Get the values to pass into the dialog

var tsk = g_form.getUniqueValue();

var ritm = g_form.getValue("request_item");

var reason = g_form.getValue("ritm.u_reason");

    var comments_text = g_form.getValue("ritm.comments");

    var short_text = g_form.getValue("ritm.short_description");

    //Initialize and open the dialog

var dialog = new GlideDialogWindow("ritm_reason_dialog"); //Instantiate the dialog containing the UI Page 'add_comments_dialog'

    dialog.setTitle("What is the reason for cancelling this request item?"); //Set the dialog title

    dialog.setPreference("reason", reason); //Pass the reason value into the dialog

dialog.setPreference("comments_text", comments_text); //Pass the comments into the dialog

    dialog.setPreference("short_text", short_text); //Pass in a short description for use in the dialog

dialog.setPreference("ritm", ritm); //Pass in a request item sys_id for use in the dialog

dialog.setPreference("tsk", tsk); //Pass the task sys_id into the dialog

    dialog.render(); //Open the dialog

}

UI Page

find_real_file.png

HTML

<g:ui_form>

  <!-- Get the values from dialog preferences -->

  <g:evaluate var="jvar_tsk"

      expression="RP.getWindowProperties().get('tsk')" />

<g:evaluate var="jvar_ritm"

      expression="RP.getWindowProperties().get('ritm')" />

<g:evaluate var="jvar_short_text"

      expression="RP.getWindowProperties().get('short_text')" />

<g:evaluate var="jvar_reason"

      expression="RP.getWindowProperties().get('reason')" />

  <g:evaluate var="jvar_comments_text"

      expression="RP.getWindowProperties().get('comments_text')" />

    <!-- Set up form fields and labels -->

    <table width="100%">

        <tr id="description_row" valign="top">

              <td colspan="2">

                    <!-- Short description value used as a label -->

                    ${jvar_short_text}

              </td>

        </tr>

      <tr>

           

    <td>

      <!-- Reason dropdown field from the RITM -->

      <g:ui_choicelist name="reason" table="sc_req_item" field="u_reason"

                value="${jvar_reason}" mandatory="true" />

                </td>

      </tr>

      <br>

  </br>

      <tr>

        <td>

    <!-- Comments text field (Contains comments from originating record as a default) -->

                <g:ui_multiline_input_field name="dialog_comments" id="dialog_comments" label="Additional comments"

                      value="${jvar_comments_text}" mandatory="true" />

            </td>

        </tr>

        <tr>

            <td colspan="2">

            </td>

        </tr>

        <tr id="dialog_buttons">

              <td colspan="2" align="right">

                    <!-- Add OK/Cancel buttons. Clicking OK calls the validateComments script -->

                    <g:dialog_buttons_ok_cancel ok="return validateComments()" ok_type="button" cancel_type="button" />

              </td>

        </tr>

  </table>

</g:ui_form>

Client Script

function validateComments() {

//This script is called when the user clicks "OK" in the dialog window

//Make sure there are comments to submit

var tsk = g_form.getUniqueValue();

var tskritm = g_form.getValue('request_item');

var reason = gel("reason").value;

var comments = gel("dialog_comments").value;

var ga = new GlideAjax('validateComments'); //Call script include to escape text

ga.addParam('sysparm_name', 'validateComments');

ga.addParam('sysparm_comments', comments);

ga.getXMLWait();

comments = ga.getAnswer(); //Set comments to escaped text

comments = trim(comments);

if (comments == "") {

  //If comments are empty, alert the user and stop submission

  alert(tskritm);

  alert("Please select a reason and enter your comments before submitting.");

  return false;

}

If there are comments, close the dialog window and submit them

GlideDialogWindow.get().destroy(); //Close the dialog window

//Set fields on parent RITM

g_form.setValue("comments", comments); //Set the "Comments" field with comments in the dialog

g_form.setValue("state", '99');

g_form.setValue("u_reason", reason);

g_form.setValue("stage", "Request Canceled");

g_form.save();

}

1 ACCEPTED SOLUTION

Brad Tilton
ServiceNow Employee
ServiceNow Employee

Hi April,



You can't actually set the values on another record using client side g_form. You could, however, use GlideAjax from the client side code in your ui page. You could pass it the sysID of the RITM then in the script include do the actual updating of the ritm record.


View solution in original post

7 REPLIES 7

Brad Tilton
ServiceNow Employee
ServiceNow Employee

I would add some logging to your script include to confirm that it's getting called and that it is setting values.


aprilz
Giga Contributor

Thank you so much for your guidance and expertise Brad!



You were correct in that my script include wasn't being called. Found the error and everything is working fine.


Hi Brad,

 

I trust this msg finds you well.

I wonder if you could point me in the right direction.

I am trying to simplify the way staff enter the 'time worked' within incidents. Rather than scrolling down to the related list and add a record i decided to do it differently. 

I want them to click a button (UI Action) which would call a popup (UI Page). The ui page will collect the details and insert a record in the 'task_time_worked' table.

is this possible?

So far i created the UI Action that calls the UI Page

 

function registercall() {
    var gdw = new GlideDialogWindow('time_worked_popup');
    gdw.setSize(750,300);
    //gdw.setPreference('table', 'task_time_worked.list');
    //gdw.setPreference('sysparm_view', 'default');
    gdw.setPreference('sys_id',g_form.getUniqueValue());
    gdw.render();
}
 
 
Then i created the UI Page as follows:
HTML (Image attached)
 
Client script section:
function closeWindow() {
    GlideDialogWindow.get().destroy();
}

function update_ticket() {
     alert(g_form.getUniqueValue());
    // var user = document.getElementById('user').value;
    var action = document.getElementById('action').value;
    var call_responded = document.getElementById('callrespon').checked;
    var call_not_responded = document.getElementById('callnotrespon').checked;
    var days = document.getElementById('days').value;
    var hours = document.getElementById('hours').value;
    var minutes = document.getElementById('minutes').value;
    var seconds = document.getElementById('seconds').value;
    var comments = document.getElementById('comments').value;
    var time_worked = calculateTimeWorked(days, hours, minutes, seconds);  
    var tck = g_form.getUniqueValue();

   
   

    var gr = new GlideRecord('task_time_worked');
    gr.initialize();
    // gr.setValue('user', user);
    gr.setValue('action', action);
    gr.setValue('u_call_responded', call_responded);
    gr.setValue('u_call_not_responded', call_not_responded);
    gr.setValue('time_worked', time_worked);
    gr.setValue('comments', comments);
    gr.setValue('task',tck);
    gr.insert();
    //closeWindow();
    //window.location.reload();
    GlideDialogWindow.get().destroy();
    //window.location.reload();
}

function calculateTimeWorked(days, hours, minutes, seconds) {
    // Convert everything to seconds and sum up
    var totalSeconds = parseInt(days) * 86400 + parseInt(hours) * 3600 + parseInt(minutes) * 60 + parseInt(seconds);
    return totalSeconds; // You can format this as needed
}
 
 
ISSUE
when i click cancel works fine
When i click OK, i know it enters as i used an alert, but it does nothing else and the window does not close.
 
would you be able to let me know why it is not working?
 
regards,
Max