Auto add row to multi row variable set

Jeff316
Kilo Guru

Hi All,

I'm using the new multi-row variable set on a catalog request form.

This catalog request is for a new server.

The intake form uses the multi-row variable set so the users can enter a list of servers with drive letter, size, etc...

I need to automatically add 1 or 2 rows for the user based on other logic.

For example, if they select Windows as the OS, then automatically in the multi-row variable set for "Drives Needed" I have to start them with a row for the C drive with some preset size attribute.

The user is free to "add" more drives needed for the server but I will automatically have added the C drive.

if they pick "unix" as the OS in another variable on the form then I need to automatically insert a different drive.

Where and how do I insert the "default" drives into the multi-row variable on the screen so to the user, it appears they already requested the default drive.

find_real_file.png

1 ACCEPTED SOLUTION

Jace Benson
Mega Sage

You just need to make a an onChange script on the item for the OS variable.

Your onChange script will probably have code like this;

var drives = [
  {
    drive: "L:",
    size: "test",
    unit: "GBs",
    tier: "Premium_LRS"
  }
];

g_form.setValue('variableSetName', JSON.stringify(drives));

View solution in original post

7 REPLIES 7

This is perfect but now, instead of having my onchange catalog client script add a default hard coded row to the mrvs, I have to have the script look up the value(s) to use from another custom table then for as many records in the custom table, add that many rows into the mrvs.  

 For example, 

There is a custom table called"recommended drives"

In this table a list of drives recommended for a particular OS; drive, size, unit, tier.

So when someone goes to the catalog item to request a new VM and selects "WIndows" or "Linux" the script needs to lookup what are the recommended drives for that OS then populates the mrvs with those recommended drives. So If I pick OS is Windows and the Recommended Drive table has 2 recommended drives C and F, then I need to bring back those 2 rows [drive,size,unit.tier] from "recommended drive" table and insert into the mrvs for "drives" so the user automatically sees that C and F drives are going to be required when I submit my server request. 

This is frustrating. 

I don't know how to lookup the values to use in the setValue.

I tried this lookup for one record in the recommended drive table just for one field called u_drive_unit.

Nothing gets set when this onchange client script runs.

function onChange(control, oldValue, newValue, isLoading) {
var REC = new GlideRecord('u_fnf_azure_request_form_recommend_drive');
REC.addQuery('description','x');
REC.query();
if (REC.next() ) {

var OSdrive = [
{
drive_letter_os: "CC",
drive_size_os: "test",
unit_os: REC.u_drive_unit,
disk_tier_os: "Premium_LRS",
sizeingbs_os: "333"
}
];

g_form.setValue('os_drive', JSON.stringify(OSdrive));
}}

Depending on where you are you may not be able to use GlideRecord that way.  It's recomended to use GlideAjax as its non-blocking.

e.g. https://blog.jacebenson.com/glideajax/

Client Script;

function onChange(control, oldValue, newValue, isLoading) {
  
  var REC = new GlideAjax('global.user316');
  REC.addParam('sysparm_name','getRecomendedDrives');
  REC.addParam('sysparm_obj',JSON.stringify({description:'x'}));
  REC.getXML(function(response){
    var responseDocument = response.responseXML.documentElement;
    var answer = responseDocument.getAttribute('answer');
    var serverObj = JSON.parse(answer);
    console.log(serverObj);
    g_form.setValue('os_drive', JSON.stringify([{
      drive_letter_os: "CC",
      drive_size_os: "test",
      unit_os: serverObj.u_drive_unit,
      disk_tier_os: "Premium_LRS",
      sizeingbs_os: "333"
    }]);
}}

// script include
var user316 = Class.create();
user316.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    // If you want to use initialize you can only if you include
    // AbstractAjaxProcessor with something like this;
    /*
    initialize: function(request, responseXML, gc) {
        global.AbstractAjaxProcessor.prototype.initialize.call(this, request, responseXML, gc);
        // Your code
    },
    */
    getRecomendedDrives: function(){
        var inputObj = JSON.parse(this.getParameter('sysparm_obj'));
        var returnObj = {
            from:"server",
            input: inputObj
        };
        var azure = new GlideRecord('u_fnf_azure_request_form_recommend_drive');
        if(azure.get('description', inputObj.description)){
          returnObj.u_drive_unit = "server os";
        }
        return JSON.stringify(returnObj);
    },
    type: 'user316'
});