“Make incremental progress; change comes not by the yard, but by the inch.”
— Rick Pitino
Last time, we had enough parts cobbled together to demonstrate that the concept actually works. Of course, all we had to show for it was some random numbers, but that told us that the specified script was being called for each row, which is what we were after. Now that we know that the basic structure is performing as desired, we can revisit the configurable Script Include component and see if we can come up with some actual use cases that might be of value to someone.
One of the questions that triggered this idea was related to comments and work notes on Incidents. Assuming that the main record in the table is an Incident, we can clone our example Script Include to create one dedicated to pulling data out of the latest comment or work note on an Incident. We can call this new Script Include ScriptedJournalValueProvider.
var ScriptedJournalValueProvider = Class.create();
ScriptedJournalValueProvider.prototype = {
initialize: function() {
},
getScriptedValue: function(item, config) {
return Math.floor(Math.random() * 100) + '';
},
type: 'ScriptedJournalValueProvider'
};
We will want to delete the example code in the getScriptedValue function and come up with our own, but other than that, the basic structure remains the same. Assuming that we want our script to be able to handle a number of attributes of an Incident Journal entry, we can use the name of the column to determine which function will fetch us our value.
getScriptedValue: function(item, config) {
var response = '';
var column = config.name;
if (column == 'last_comment') {
response = this.getLastComment(item, config);
} else if (column == 'last_comment_by') {
response = this.getLastCommentBy(item, config);
} else if (column == 'last_comment_on') {
response = this.getLastCommentOn(item, config);
} else if (column == 'last_comment_type') {
response = this.getLastCommentType(item, config);
}
return response;
}
This way, we can point to this same script in multiple columns and the name of the column will determine which value from the last comment or work note gets returned.
Since all of the functions will need the data for the last entry, we should create a shared function that they all can leverage to obtain the record. As with many things on the ServiceNow platform, there are a number of ways to go about this, but for our demonstration purposes, we will read the sys_journal_field table looking for the last entry for the Incident in the current row.
getLastJournalEntry: function(sys_id) {
var journalGR = new GlideRecord('sys_journal_field');
journalGR.orderByDesc('sys_created_on');
journalGR.addQuery('name', 'incident');
journalGR.addQuery('element_id', sys_id);
journalGR.setLimit(1);
journalGR.query();
journalGR.next();
return journalGR;
}
Now that we have a common way to obtain the GlideRecord for the latest entry, we can start building our functions that extract the requested data value. Here is the one for the comment text.
getLastComment: function(item, config) {
var response = '';
var journalGR = this.getLastJournalEntry(item.sys_id);
if (journalGR.isValidRecord()) {
response = journalGR.getDisplayValue('value');
}
return response;
}
The others will basically be copies of the above, modified to return different values based on their purpose. The whole thing, all put together, now looks like this.
var ScriptedJournalValueProvider = Class.create();
ScriptedJournalValueProvider.prototype = {
initialize: function() {
},
getScriptedValue: function(item, config) {
var response = '';
var column = config.name;
if (column == 'last_comment') {
response = this.getLastComment(item, config);
} else if (column == 'last_comment_by') {
response = this.getLastCommentBy(item, config);
} else if (column == 'last_comment_on') {
response = this.getLastCommentOn(item, config);
} else if (column == 'last_comment_type') {
response = this.getLastCommentType(item, config);
}
return response;
},
getLastComment: function(item, config) {
var response = '';
var journalGR = this.getLastJournalEntry(item.sys_id);
if (journalGR.isValidRecord()) {
response = journalGR.getDisplayValue('value');
}
return response;
},
getLastCommentBy: function(item, config) {
var response = '';
var journalGR = this.getLastJournalEntry(item.sys_id);
if (journalGR.isValidRecord()) {
response = journalGR.getDisplayValue('sys_created_by');
}
return response;
},
getLastCommentOn: function(item, config) {
var response = '';
var journalGR = this.getLastJournalEntry(item.sys_id);
if (journalGR.isValidRecord()) {
response = journalGR.getDisplayValue('sys_created_on');
}
return response;
},
getLastCommentType: function(item, config) {
var response = '';
var journalGR = this.getLastJournalEntry(item.sys_id);
if (journalGR.isValidRecord()) {
response = journalGR.getDisplayValue('element');
}
return response;
},
getLastJournalEntry: function(sys_id) {
var journalGR = new GlideRecord('sys_journal_field');
journalGR.orderByDesc('sys_created_on');
journalGR.addQuery('name', 'incident');
journalGR.addQuery('element_id', sys_id);
journalGR.setLimit(1);
journalGR.query();
journalGR.next();
return journalGR;
},
type: 'ScriptedJournalValueProvider'
};
Now that we have a Script Include to utilize, we need to put together a new page so that we can configure it to make use of it so that we can test it out. Let’s make a quick copy of the page that we were using for testing last time and call it scripted_value_test. Also, let’s make a quick copy of the test configuration script that we were using earlier and call it ScriptedValueConfig.
var ScriptedValueConfig = Class.create();
ScriptedValueConfig.prototype = Object.extendsObject(ContentSelectorConfig, {
initialize: function() {
},
perspective: [{
name: 'all',
label: 'all',
roles: ''
}],
state: [{
name: 'all',
label: 'All'
}],
table: {
all: [{
name: 'incident',
displayName: 'Incident',
all: {
filter: 'caller_idDYNAMIC90d1921e5f510100a9ad2572f2b477fe^active=true',
fields: 'number,opened_by,opened_at,short_description',
svcarray: [{
name: 'last_comment_on',
label: 'Last Comment',
heading: 'Last Comment',
script: 'global.ScriptedJournalValueProvider'
},{
name: 'last_comment_by',
label: 'Last Comment By',
heading: 'Last Comment By',
script: 'global.ScriptedJournalValueProvider'
}],
aggarray: [],
btnarray: [],
refmap: {
sys_user: 'user_profile'
},
actarray: []
}
}]
},
type: 'ScriptedValueConfig'
});
Now let’s pull up our new page in the Service Portal Designer and point the table widget to our new configuration script.
Once we save that, we can pop over to the Service Portal and pull up our new page to try it out.
Beautiful! Our new scripted value provider Script Include was called by the core SNH Data Table widget and it returned the requested values, which were then displayed on the list with all of the other standard table columns. That wasn’t so hard, now was it?
Of course, we still have a couple more wrapper widgets to modify (and test!), and I would like to produce another example, maybe something to do with catalog item variables, but I think we are close. One thing I see that I never noticed before, though, is that the added columns don’t quite line up with the original columns. Maybe it is a CSS thing, or maybe it is something a little more diabolical, but I want to take a look at that and see what is going on there. All of the data in the columns should be displayed consistently; I don’t like it when things don’t all line up correctly. I need to figure out what is going on there and see what I can do about it.
Anyway, we still have a little more work to do before we can wrap this all up into a new Update Set and post a new version out on Share, but we will keep plugging along in our next installment.