A

couple of days ago I wrote about some cool ways that you can show system list information in GlideDialogWindow popups from a form. As promised, here’s another article showing some other ways that you can use GlideDialogWindow. If you want to see all of the articles I’ve written about GlideDialogWindow and popups in Service-now just use the tags at the bottom of this article.

In this article I’ll show you how you can use GlideDialogWindow to update records from a list with a multiple update or a form with an update or insert on a single record anywhere in the system.


Displaying a form popup from a form

Here’s an example I’ve used in the past that shows how you can use ‘GlideDialogWindow’ to show a popup from a form that you can use to update any record in the system (or make an insert into any table in the system). This example is a popup that shows required closure information when a UI action button is clicked.

1Create a new view on the table that has the form that you want to appear in the popup. For the new view you’ll just want to make sure that you include only the fields that are absolutely necessary to allow the user to make an update. In almost every case, your default form view will have way more information than is required. For this example I just added a few fields that would allow the user to enter resolution information about the incident.

2Create a UI action button that you can click to make the popup appear. Make sure to check the ‘Client’ checkbox and provide a value of ‘showMyForm()’ (or whatever the name of your ‘onclick’ function is) in the ‘OnClick’ field. For this example, I’ve added a condition of ‘!current.isNewRecord()’ so that the button doesn’t show up unless it appears on a valid record. Replace the reference to ‘ResolveDialog’ below with the name of your new view created in step 1 above.

‘Resolve Dialog’ UI Action
Name: Resolve Dialog
Table: Incident
Client: True
Form Button: True
OnClick: showMyForm()
Condition: !current.isNewRecord()
Script:

function showMyForm(){
   //Get the table name and sys_id of the record
   var tableName = g_form.getTableName();
   var sysID = g_form.getUniqueValue();

   //Create and open the dialog form
   var dialog = new GlideDialogForm('Update incident', tableName); //Provide dialog title and table name
   dialog.setSysID(sysID); //Pass in sys_id to edit existing record, -1 to create new record
   dialog.addParm('sysparm_view', 'ResolveDialog'); //Specify a form view
   dialog.addParm('sysparm_form_only', 'true'); //Add or remove related lists
   dialog.render(); //Open the dialog
}

Thanks to the forums I also found that if you need to pass values into the dialog from your original form, you can set a callback function to pass and set those values into your dialog like this…

function showMyForm(){
   //Get the table name and sys_id of the record
   var tableName = g_form.getTableName();
   var sysID = g_form.getUniqueValue();

   //Create and open the dialog form
   var dialog = new GlideDialogForm('Update incident', tableName); //Provide dialog title and table name
   dialog.setSysID(sysID); //Pass in sys_id to edit existing record, -1 to create new record
   dialog.addParm('sysparm_view', 'ResolveDialog'); //Specify a form view
   dialog.addParm('sysparm_form_only', 'true'); //Add or remove related lists

   dialog.setLoadCallback(function(iframeDoc) {
   // To get the iframe: document.defaultView in non-IE, document.parentWindow in IE
   var dialogFrame = 'defaultView' in iframeDoc ? iframeDoc.defaultView : iframeDoc.parentWindow;

   dialogFrame.g_form.setValue('close_notes', 'Hello world!');
   dialogFrame = null;
   });

   dialog.render(); //Open the dialog
}

3Since this form will include ALL of the buttons that normally display on the form, you’ll probably also want to add a client script that runs against the view you created and removes any unnecessary buttons. Typically you’ll just want to have the ‘Update’ button visible for something like this. An ‘onLoad’ client script that runs against your new view with a script like this should do the trick.

‘ResolveDialog remove buttons’ Client Script
Note that I’ve also added a couple of lines to set the value of the ‘Incident State’ field and make it read only. We don’t want people to modify the state from this popup form, but it needs to be set so that our UI Policy will display the closure fields and make them mandatory. Also note that I’ve used the ‘State’ field instead of ‘Incident State’ in my examples here. If you use ‘Incident State’ you’ll need to modify the script.

Name: ResolveDialog remove buttons
Table: Incident
Global: False
View: ResolveDialog
Type: onLoad

function onLoad() {
   //Set the incident state to Resolved
   g_form.setValue('state', 6);
   g_form.setReadonly('state', 'true');

   //Remove all buttons except for the 'Update' button
   var items = $$('BUTTON').each(function(item){
      if(item.id != 'sysverb_update'){
         item.hide();  
      }
   });
}

Here’s the end result. A simple, nice-looking form that allows you to make changes to any record in the system without navigating away from the record you’re currently viewing!

Displaying a form popup from a list (multiple updates with one action)

Here’s an example I’ve used in the past that shows how you can use ‘GlideDialogWindow’ along with the ‘showQuickForm’ method to show a popup from a list that you can use to update multiple records in a list with a single action.

The biggest drawback to this method is that it DOES NOT run client scripts or UI policies. Because of this, you’ll need to put a little more thought into exactly how you use it to present fields. It is also designed to ONLY work as a list action. The ‘showQuickForm’ method has also had some issues in the past (including a recent one with the ListV2 plugin). These issues have been resolved in the Fall 2010 Service-now release. If you’re seeing issues, chances are you need to upgrade.

1Create a new view on the table that has the form that you want to appear in the popup. For the new view you’ll just want to make sure that you include only the fields that are absolutely necessary to allow the user to make an update. In almost every case, your default form view will have way more information than is required. For this example I just added a few fields that would allow the user to enter assignment information about the incident.

2Create a UI action list choice menu option that you can click to make the popup appear. Replace the reference to ‘assignment’ in the condition below with the name of your new view created in step 1 above. You’ll also need to make sure that the ‘Action Name’ of your UI action matches the action name passed into your ‘OnClick’ function. Notice that I have ‘reassign’ for both the ‘Action Name’ and as a parameter going into the ‘showQuickForm’ function.

‘Reassign’ UI Action
Name: Reassign
Table: Incident
Action Name: reassign
Client: True
OnClick: showQuickForm(‘assignment’, ‘reassign’)
List Choice: True
Condition: gs.hasRole(‘itil’)
Script:

try{
   //Make sure State gets changed to 'Active'
   current.state = 2;
   current.update();
}catch(e){}

That’s it! There’s no need to remove extra buttons or form elements in this case. The ‘showQuickForm’ function takes care of that for you. You should be able to go to your record list, check a few records, and click the UI action choice option to see a popup that allows you to update multiple records with entries from your custom form in a single action.