logo

NJP

Accessing Multi-Row Variable Sets in a Record Producer

Import · Oct 22, 2018 · article

Hi All,

I recently had the displeasure of figuring out how the heck the new multi-row variable sets work, and how to mash the data into a record producer script to get something useful on the other side. There doesn't seem to be much (if any) documentation on this, so a lot of trial and error discovered the following:

  • Printing the variable (in my case 'producer.contact_details') outputs something that looks like an array of objects
  • The typeof this variable is an object, so .length and forEach won't run on it
  • The individual objects within the collection can be accessed through IDs, e.g. 'producer.contact_details[0].email', however printing just 'producer.contact_details[0]' returns null.
  • There are a number of undocumented methods on the object, discovered through a 'help' method (producer.contact_details.help()):
****************************************************************************************************
Class: com.glide.catalog.component.variables.runtime.models.table.impl.TableVariableNode
----------------------------------------------------------------------------------------------------
 Interface: com.glide.catalog.component.variables.models.table.api.ITableVariableNode
----------------------------------------------------------------------------------------------------
  void deleteRow(int)
  com.glide.catalog.component.variables.models.table.api.ITableVariableRowNode addRow()
  java.util.Set getQuestionIds()
  com.glide.catalog.component.variables.models.table.api.ITableVariableRowNode getRow(int)
  int getRowCount()
  java.util.List getRows()
  com.glide.catalog.component.variables.models.table.api.ITableVariableCellNode[] getCells(java.lang.String)
====================================================================================================
Property Based Accessors/Mutators
  Default Getter: com.glide.catalog.component.variables.models.table.api.ITableVariableCellNode[] getCells(java.lang.String)
  Default Setter: void setCellValues(java.lang.String,java.lang.Object)
  Default Setter: void setRows(java.lang.Object)

****************************************************************************************************
  • I couldn't get much out of the getRow/getRows and getCells methods, but getRowCount thankfully did return an accurate count of rows.

So the end result of all this is the following to add your rows to the comments of a newly created incident:

var commentTmp = '[code]<p>Customer has requested the following new contacts:</p>';
var contactArr = producer.contact_details;
var contactCnt = producer.contact_details.getRowCount();
if (contactCnt > 0) {
    for (i = 0; i < contactCnt; i++) {
        commentTmp += '<p><b>Name:</b> '+contactArr[i].user_name+
        '<br><b>Email:</b> '+contactArr[i].email+
        '<br><b>Mobile:</b> '+contactArr[i].phone_mob+
        '<br><b>Direct:</b> '+contactArr[i].phone_office+
        '<br><b>Type:</b> '+contactArr[i].type+'</p><br>';          
    }
    commentTmp += '[/code]';
}
current.comments = commentTmp;

I'm sure there's a much neater way to do this, but through brute force this is the best I could find, and hoping it helps someone else in a similar situation.

If anyone knows of a place where documentation for this is hidden away, I'd greatly appreciate it!

View original source

https://www.servicenow.com/community/itsm-articles/accessing-multi-row-variable-sets-in-a-record-producer/ta-p/2299546