Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to Backfill new field Record Producer records with Variables

Wayne Miller
Tera Guru

@Ankur Bawiskar 

Following up on discussion in Thread: Copy variable details to RITM description 

 

We've applied Ankur's solution in a Business Rule to fetch Variable Values from new RP records ("Cases" for us) and populate a new String field "Case Variable Summary", see below:

 

var variables = current.variables.getElements();

var str = '';
for (var i=0;i<variables.length;i++) {
var question = variables[i].getQuestion();
var variableLabel = question.getLabel();
var variableValue = question.getDisplayValue();
str = str + variableLabel + ' - ' + variableValue + '\n';
}

current.case_variable_summary = str;

 

What we need to do is to apply this logic to existing records so we can backfill the new field "case_variable_summary" in each Case we have logged.

 

We started down the road of GlideRecord:

 

 

var gd = new GlideRecord('case');
var a = gd.addNullQuery('case_variable_summary');
a.addCondition('active','true');
gd.query();
while(gd.next())
{
    // this is where we need the BR Logic
}

 

 

But it's proved fruitless so far.  Can you help?

1 ACCEPTED SOLUTION

We got there...

 

var gd = new GlideRecord('case');
var a = gd.addNullQuery('case_variable_summary');
a.addCondition('active','true');
gd.query();
while(gd.next())
{
    var varSum = gd.variables.getElements();

    var str = '';
    for (var i=0;i<varSum.length;i++) {
    var question = varSum[i].getQuestion();
    var variableLabel = question.getLabel();
    var variableValue = question.getDisplayValue();
    str = str + variableLabel + ' - ' + variableValue + '\n';
}
   
gd.case_variable_summary = str;
gd.update();
}

View solution in original post

2 REPLIES 2

Wayne Miller
Tera Guru

Update, we're getting somewhere...

var gd = new GlideRecord('case');
var a = gd.addNullQuery('case_variable_summary');
a.addCondition('active','true');
gd.setLimit(5); // Set value for testing purposes only
gd.query();
while(gd.next())
{
    var varSum = gd.variables.getElements();

    var str = '';
    for (var i=0;i<varSum.length;i++) {
    var question = varSum[i].getQuestion();
    var variableLabel = question.getLabel();
    var variableValue = question.getDisplayValue();
    str = str + variableLabel + ' - ' + variableValue + '\n';
}
   
    gs.info(gd.number + " - " + str); // This returns the variable values and the RP Number in one field.
}

 

We just need to have the return value update the case_variable_summary field.   So watch this space... 😄

We got there...

 

var gd = new GlideRecord('case');
var a = gd.addNullQuery('case_variable_summary');
a.addCondition('active','true');
gd.query();
while(gd.next())
{
    var varSum = gd.variables.getElements();

    var str = '';
    for (var i=0;i<varSum.length;i++) {
    var question = varSum[i].getQuestion();
    var variableLabel = question.getLabel();
    var variableValue = question.getDisplayValue();
    str = str + variableLabel + ' - ' + variableValue + '\n';
}
   
gd.case_variable_summary = str;
gd.update();
}