Finding out if an element is available in an array

Jon Smith0
Tera Contributor

What I'm trying to accomplish is to make a util script include that will be used to update a table. While I could create a Util script for each use case I figure it would be easier to create one then if possible the script would be able to determine if the array passed to the function would be able to tell what elements of the array were passed.

 

example there are 17 fields in this table but I'm only passing it label, value, active, sequence, name. If someone else wants to come along and also wants to add records to this table but wants to also push the hint field I don't want them adding it to the script which would break my code.

 

I have tried doing something like:

if(updateValue[i].indexOf('hint') != -1){
  createChoice.hint = updateValue[i].hint;
}

 

However as I feared this seems to only be looking for the values of the array instead of the elements of the array. Is there a way to see if the element exists in the array passed to the function and if it does then it will be added to the gr?

1 ACCEPTED SOLUTION

Hitoshi Ozawa
Giga Sage
Giga Sage

@Jon Smith0 How is the array structured? Should make it as an array of objects.

e.g.

var updateValueArray = [{
    label: 'testLabel',
    value: 'testValue',
    active: true,
    sequence: 1,
    name: 'testName',
	hint: 'testHint'
}];
for (var i = 0; i < updateValueArray.length; i++) {
    if ('hint' in updateValueArray[i]) {
        gs.error("error. hint exists");
    } else {
        gs.info(updateValueArray[i].label);
    }
}

Execution result:

HitoshiOzawa_0-1730952404314.png

 

View solution in original post

3 REPLIES 3

Abhay Kumar1
Giga Sage

@Jon Smith0 What I understood you need both key and value as well , please try something like below to have key details if works for you:

var util = new UtilScript();

util.updateTable('your_table_name', {

    label: 'Sample Label',

    value: 'Sample Value',

    active: true,

    sequence: 1,

    name: 'Sample Name',

    hint: 'Optional hint'

});

 

And to validate key you can use :

updateTable: function(tableName, updateValues) {

        var gr = new GlideRecord(tableName);

        gr.initialize();  Object.keys(updateValues).forEach(function(field) {

            if (gr.isValidField(field))

gr[field] = updateValues[field];

        });

 

     //   gr.insert(); // if required uncomment

    },

You can modify script for any additional code required to be added or removing table update as well.

Hope this will help you.

Hitoshi Ozawa
Giga Sage
Giga Sage

@Jon Smith0 How is the array structured? Should make it as an array of objects.

e.g.

var updateValueArray = [{
    label: 'testLabel',
    value: 'testValue',
    active: true,
    sequence: 1,
    name: 'testName',
	hint: 'testHint'
}];
for (var i = 0; i < updateValueArray.length; i++) {
    if ('hint' in updateValueArray[i]) {
        gs.error("error. hint exists");
    } else {
        gs.info(updateValueArray[i].label);
    }
}

Execution result:

HitoshiOzawa_0-1730952404314.png

 

Hitoshi Ozawa
Giga Sage
Giga Sage

Script Include to update table.

Script Include

var UpdateTable = Class.create();
UpdateTable.prototype = {
    initialize: function() {},

    updateTable: function(tableName, UpdateValueObj) {
        if ('hint' in UpdateValueObj) {
            gs.error("error. hint exists");
        } else {
            var gr = new GlideRecord(tableName);
            if (gr.get(UpdateValueObj['sys_id'])) {
                for (var key in UpdateValueObj) {
                    if (key != 'sys_id') {
                        gr[key] = UpdateValueObj[key];
                    }
                }
                gr.update();
            }
        }
    },
    type: 'UpdateTable'
};

Sample Scripts - Background

var updater = new UpdateTable();

// Define the table name and update values
var tableName = 'incident'; // Specify the table name
var updateValues = {
    sys_id: '85071a1347c12200e0ef563dbb9a71c1', // Replace with the sys_id of the record you want to update
    short_description: 'Updated short description',
    urgency: 2,
	hint: 'illegal definition'
};

updater.updateTable(tableName, updateValues);

gs.log('Background script executed: UpdateTable script include has been called.');

Execution result:

1. When "hint" is defined.

HitoshiOzawa_0-1730954572916.png

2. When "hint" is not defined

HitoshiOzawa_1-1730954612047.png