Generate SOAP Variables

Web based applications have traditionally been notorious for having tedious, repetitive user interfaces that have made them difficult to adopt. As the web has evolved, so has the interfaces. ServiceNow has always impressed me on the simplicity of the UI and how it can be used to reduce manual interaction.

It is rare to find a section of the UI that is repetitive and tedious. However, I realized the other day that I was spinning wheels unnecessarily with the UI and a simple fix was available.

The issue lies with the SOAP Message Generator (The REST Message Generator has the same issue, but can be fixed just as easily). When you consume a WSDL, the SOAP Message tool will auto generate all of the available functions as well as a template for the SOAP Envelope. In the SOAP Envelope, ServiceNow intelligently determines elements that may require data for a SOAP submission and it populates those elements with a ${variable_name} string. These are special ${} style variables that can be used to insert dynamic data into your SOAP Message.

Example of SOAP Variables

Sample SOAP function envelope that is auto generated by ServiceNow and contains special ${}-style variables that are placeholders for dynamic content.

So far, so good, right? This is a big help, but there is another step that I felt just got left out. If you want to either test the SOAP message function within the tool, or take advantage of the “Preview script usage” feature to autogenerate sample code for the web service call, you have to first create SOAP Message Parameters for each of the ${} style variables in the envelope.

This is typically ok if you have two or three tag elements that will have dynamic content. However, there are some web services where you could be submitting more than 20 pieces of data.

In order to make this experience a little easier to deal with, you can create a UI Action that will read the SOAP envelope and generate all of the variables for you.

UI Action Details

Name: Generate SOAP Message Parameters
Table: SOAP Message Function [sys_soap_message_function]
Action Name: gen_soap_params
Form Link: Checked
Active: Checked
Show insert: Checked
Show update: Checked
Comments: Read the SOAP Envelope and generate SOAP Message Parameters from all of the ${} variables found in the envelope.
Condition: current.envelope != “”
Script:

1
2
3
4
5
6
7
8
9
10
11
var variables = current.envelope.match(/\$\{.*?\}/g);

for(var key in variables){
  var p = new GlideRecord("sys_soap_message_parameters");
  p.initialize();
  p.name = variables[key].replace(/\$\{/,"").replace(/\}/,"");
  p.soap_function = current.sys_id;
  p.insert();
}

action.setRedirectURL(current);

Result

Auto Generate SOAP Variables Result

The above picture shows part of a SOAP Envelope that contains 72 variable elements. Normally you would have to create all 72 variables as SOAP Message Parameters. However, with the click of the new “Generate SOAP Message Parameters” UI Action link, those parameters get auto populated for you.

Download the UI Action

Click the following link to download the UI Action record to easily install it into your instance: Auto Generate SOAP Parameters