Dave
Dave

Reputation: 1277

ServiceNow retrieving objects from array

I am working in ServiceNow and have an amateur coding question when it comes to objects, arrays, and how to access the elements. Below, I start with an empty array and populate it with objects. Now that I have an array with objects in them, my question is how do I access the different elements? I want to be able to generate a table where the columns are number, short_desc, and url, and the rows are produced dynamically. How am I able to accomplish this?

data.list = [];
var ka = new GlideRecord('x_81991_knowledge');
    ka.addQuery('kb_category.label', 'Benefits');
    ka.query();
        while(ka.next()) {
            data.list.push({
                number: ka.getValue('number'),
                short_desc: ka.getValue('short_description'),
                url:'kb_view.do?sysparm_article=' + ka.getValue('number')
          });
}

Upvotes: 1

Views: 10966

Answers (4)

Rupali Bhatnagar
Rupali Bhatnagar

Reputation: 21

Also, if you would like to get the url of a record in servicenow, I'd recommend to use ka.getLink(). Refer:getLink()

Upvotes: 1

Ratul Arora
Ratul Arora

Reputation: 31

This is how you can get the data using obj with an array:

data = []; // array
var ka = new GlideRecord('x_81991_knowledge');
ka.addQuery('kb_category.label', 'Benefits');
ka.query();
while (ka.next()) {
    var list = {}; // object
    list.number = ka.getValue('number');
    list.short_desc = ka.getValue('short_description');
    list.url = 'kb_view.do?sysparm_article=' + ka.getValue('number');
    data.push(list);
}

Upvotes: 0

Howard Elton
Howard Elton

Reputation: 155

Here is an example of how to output the array as a flat table. Run this as a "background script", because it uses "gs.print" to output the results. Note that I had to add the first line just set up the "data" object in the script, but you would not need that if it already exists in your code.

var data = {}

data.list = [];
var ka = new GlideRecord('x_81991_knowledge');
    ka.addQuery('kb_category.label', 'Benefits');
    ka.query();
        while(ka.next()) {
            data.list.push({
                number: ka.getValue('number'),
                short_desc: ka.getValue('short_description'),
                url:'kb_view.do?sysparm_article=' + ka.getValue('number')
          });
}

for(var i=0; i < data.list.length; i++){
   gs.print("Number: " + data.list[i].number + ", Short Description: " + data.list[i].short_desc + ", URL: " + data.list[i].url);
}

Upvotes: 0

Tim Woodruff
Tim Woodruff

Reputation: 601

So, you've got an object: data, and in that object is an element: list. That element is an array, which contains other objects.

Just as Objects in JavaScript contain properties with names, arrays are just like objects with properties that all have sequentially numbered names.

For example, if you have an array like var fruitz = ['apple', 'banana', 'hammer', 'pear'];, it's very similar to having an object that looks like this:

var fruitz = {
    0:'apple',
    1:'banana',
    3:'hammer',
    4:'pear'
};

In either example, you can access the element with the value 'banana' with: fruitz[1];.

There are of course, several differences - including the fact that, of course, objects don't normally have properties with sequential numerical names! - this is JUST a good way to visualize how one would access array elements.

Array elements in JS do retain their order, so an element in position 3 (the fourth element - remember, arrays use a "zero-based index") will remain at position 3.

You could do a for-loop to iterate over and get elements from the array like so:

var i;
for (i = 0; i < fruitz.length; i++) {
    gs.info(fruitz[i] + ' is element number ' + i + ' in the array.');
}

There are some great resources online for learning Angular, which is definitely critical if you're looking to understand how the service portal works on the front-end.

Upvotes: 2

Related Questions