Adding additional record to a GlideRecord object

jbyates
Kilo Explorer

I'm attempting to query the category->catalog items join table to get a set of request items that i can then pass to a list and display on a page. All I need is the request items. I'm trying to query the sc_cat_item_category table to get my list and then construct a GlideRecord on the sc_cat_item table from those records. The problem is I am unable to find a method to add items to a GlideRecord. I have tried "push" but this does not seem to add any records. Is there a built in way to handle this process?

Here is an example of what I have tried:




//query the catalog item and category join table
var categoryItems = new GlideRecord('sc_cat_item_category');
categoryItems.addQuery('sc_category', categoryID);
categoryItems.addActiveQuery();
categoryItems.orderBy('order');
categoryItems.query();
//build a gliderecord consisting of the found catalog items
var catalogItems = new GlideRecord('sc_cat_item');
while(categoryItems.next()){
catalogItems.push(categoryItems.sc_cat_item);
}

return catalogItems;
}

5 REPLIES 5

geoffcox
Giga Guru

From the way you're describing the situation, I don't think you need to do any scripting at all.

Try just displaying the list view of sc_cat_item filtered for category = categoryID.

In the filter menu, just type "sc_cat_item.list"
Then add a line to the filter of "Category" "is"

Alternatively, you can access this list by URL:



https://yourinstance.service-now.com/sc_cat_item_list.do?sysparm_query=category%3Dd258e415c611227a0013072db7288863


(Use your desired category ID in the URL).

If used in a module, you would reference locally:


nav_to.do?uri=sc_cat_item_list.do?sysparm_query=category%3Dd258e415c611227a0013072db7288863


If you really want a gliderecord of sc_cat_item for the particular category, then you can just do the following:



var catalogItems = new GlideRecord('sc_cat_item');
catalogItems.addQuery('category',categoryID);
catalogItems.query();

Now you have a glideRecord with your desired information, but then what? 🙂 I'm pretty sure you don't want to insert additional records into the sc_cat_item table.

I hope this helps.

Cheers,
Geoff.


jbyates
Kilo Explorer

I'm using this to display in a list on a content page and unfortunately the items could be in multiple categories, hence why i need to use the join table.


geoffcox
Giga Guru

You can add OR conditions to the filter to select more categories. In a query, you can use "addOrCondition" to select multiple categories:



var catalogItems = new GlideRecord('sc_cat_item');
var ciOrCond = catalogItems.addQuery('category',category1ID);
ciOrCond.addOrCondition('category',category2ID);
catalogItems.query();


Either that or I really don't understand what you're trying to do.

In your original post it appears you're trying to push GlideRecords of one class into a different class. I don't want to sound mean, but this is sure to be a disaster. 🙂 A GlideRecord for one class does not have the same fields as a GlideRecord for a different class.

What information do you have before you do your query? A list of categories? Once you have your data, how are you displaying it?


jbyates
Kilo Explorer

Basically i need to create a GlideRecord object with a list of the matching catalog items from the sc_cat_item_category table. The problem is when you try and query the table you just get the results from that table and not a list that only contains the catalog item objects. I need it to be a GlideRecord so I can use the built in CMS formatting templates.

The problem with using one of the examples you listed is that my catalog items can be in multiple categories so we need to use the item - category table.