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

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.


Thank you, Brad!



I'm very new to scripting and used script include to build a selection list in a catalog item but not to actually set values.



I've attempted to use GlideAjax with no success. Can you provide some direction on where I'm going wrong?



Here's my script include...



var CancelRITM = Class.create();


CancelRITM.prototype = Object.extendsObject(AbstractAjaxProcessor, {


getCanRITM: function(){


  var ritm = this.getParameter('sysparm_tskritm');


  var comments = this.getParameter('sysparm_comments');


  var reason = this.getParameter('sysparm_reason');


 


  var ri = new GlideRecord('sc_req_item');


  ri.addQuery('sys_id', ritm);


  ri.query();


 


  while(ri.next()){


    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();


  }


},



type: 'CancelRITM'


});




Here's my client script on the UI page...



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("Please select a reason and enter your comments before submitting.");


  return false;


}



var cr = new GlideAjax('CancelRITM');


cr.addParam('sysparm_name', 'CancelRITM');


cr.addParam('sysparm_tskritm', tskritm);


cr.addParam('sysparm_comments', comments);


cr.addParam('sysparm_reason', reason);


cr.getXML();


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


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



//var ritm = new GlideRecord('sc_req_item');


//ritm.addQuery('sys_id', tskritm);


//ritm.addQuery('active','true');


//ritm.query();


//if (ritm.next())


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



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


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


g_form.save();


}


Brad Tilton
ServiceNow Employee
ServiceNow Employee

In your script include you'll want to avoid g_form and do ri.setValue() and then ri.update() to save the record.


I was hoping it would as easy as switching out the g_form to ri.setValue() and ri.update() but no such luck. I must be missing something somewhere else.