Populate Table field type value based on another field.

Mark Wood
Tera Contributor

Hello Experts,

I have created two fields first one is the model which is the list-type field which refers to the cmdb_model table.

The second field I have created which is the table field which has a datatype table now based on the product model I want to populate the table.

If we select more than one model in the same category, the table does not change.
But if we select a CI from a different category, the Table field displays the lowest level table that contains all the selected CIs.
For example, if you select two PCs and one laser printer, the Table field changes to Hardware [cmdb_ci_hardware], because that table includes computers and printers.
If you then add a computer rack, the Table field changes to Configuration Item [cmdb_ci], which contains all CIs.

how can I achieve this please guide me on this thank you .

 

3 REPLIES 3

Tushar
Kilo Sage
Kilo Sage

Hi @Mark Wood 

 

I think you can use a GlideRecord Script Include.

This below script include will get the lowest level table that contains all of the selected CIs -

 

 

function getLowestLevelTable(models) {
  // Get the tables for the selected models.
  var tables = [];
  for (var i = 0; i < models.length; i++) {
    var model = models[i];
    var table = model.sys_class_name;
    tables.push(table);
  }

  // Find the lowest level table.
  var lowestLevelTable = tables[0];
  for (var i = 1; i < tables.length; i++) {
    var table = tables[i];
    if (table.includes(lowestLevelTable)) {
      lowestLevelTable = table;
    }
  }

  // Return the lowest level table.
  return lowestLevelTable;
}

 

 

In order to use this script include, now you can create a business rule on the table field that you created.

The BR should set the value of the table field to the value returned by the getLowestLevelTable() function.

For eg - if you created a table field called table, refer following business rule:

 

 

(function executeRule(current, previous /*null when async*/) {
  // Get the selected models.
  var models = current.getValue('model');

  // Get the lowest level table that contains all of the models.
  var lowestLevelTable = getLowestLevelTable(models);

  // Set the value of the table field.
  current.setValue('table', lowestLevelTable);
})(current, previous);

 

 

Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!

Regards,
Tushar

Hello @Tushar  ,

I have written the code below, taking your script as a reference. I am currently testing it with an after-insert business rule. However, it does not seem to be working. Could you please guide me on what mistake I might be making? Thank you for your assistance.

 

Script Include Code:

var Populatetable = Class.create();
Populatetable.prototype = {
initialize: function() {},
getLowestLevelTable: function(models) {
// Get the tables for the selected models.
var tables = [];
for (var i = 0; i < models.length; i++) {
var model = models[i];
var table = model.sys_class_name;
tables.push(table);
}

// Find the lowest level table.
var lowestLevelTable = tables[0];
for (var j = 1; j < tables.length; j++) {
var table1 = tables[j];
if (table1.includes(lowestLevelTable)) {
lowestLevelTable = table1;
}
}

// Return the lowest level table.
return lowestLevelTable;
},

 


type: 'Populatetable'
};

 

 

 

After Business Rule Script:

 

(function executeRule(current, previous /*null when async*/) {

var models = current.getValue('u_ma_fld_model');

// Get the lowest level table that contains all of the models.
var lowestLevelTable = getLowestLevelTable(models);

// Set the value of the table field.
current.setValue('u_ma_fld_table', lowestLevelTable);

})(current, previous);

 

@Ankur Bawiskar can you please guide me on this.