The CreatorCon Call for Content is officially open! Get started here.

Querying "sc_cat_item" table

madhusudanshett
Kilo Contributor

Hello SNC

I am querying "sc_cat_item" table to display the items on a page based on Category sys_id.

<g:evaluate jelly="true" >

var grCatItem = new GlideRecord('sc_cat_item');

grCatItem.addQuery('active', true);

grCatItem.addQuery('category', grSC.sys_id); <!-- grSC.sys_id is category sys_id from previous query. 'category' is the field on the form -->

<!-- Condition required here -->

grCatItem.orderBy('order');

grCatItem.query();

grCatItem;

</g:evaluate>

It checks only for the main Category on the form.

i.e.

1.png

It doesn't check for the categories in the View/Related List

2.png

New1.png

New2.png

If item is listed under many categories then one category will show on the form rest on the related list/view.

So I want to query the related list/view to check whether category sys_id matches with any category in the related list/view.

Please help me to complete this requirement.

Thanks in Advance.

1 ACCEPTED SOLUTION

Hi Madhusudan,



I think Alexander was on the right track using the many-to-many table... but he has the logic backwards for your purpose.



Try this instead for your addQuery() to include a search for the m2m related items as well (I've made bold my changes):



<g:evaluate jelly="true" >



//Gather all those from the Related Items list


var grRelated = new GlideRecord('sc_cat_item_category');


grRelated.addQuery('sc_category', grSC.sys_id);


grRelated.query();


var relatedItems = '';


var sep = '';


while(grRelated.next()){


        relatedItems += sep + grRelated.sc_cat_item.sys_id;


        sep = ',';


}



var grCatItem = new GlideRecord('sc_cat_item');


grCatItem.addQuery('active', true);


grCatItem.addQuery('category=' + grSC.sys_id + '^ORsys_idIN' + relatedItems);



<!-- Condition required here -->


grCatItem.orderBy('order');


grCatItem.query();


grCatItem;


</g:evaluate>







See how that works for you.




Thanks,


-Brian


View solution in original post

5 REPLIES 5

Sure, will check it out.   Please mark this one as *Correct* if that was the solution.



Thanks,


-Brian