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

I want to manage the visibility of catalog items only to the subscribers to the Service Offerings

pdurgapavan
Tera Contributor

I would like to show the catalog items to the users who subscribed to the Service Offerings. Let's say On the service_offering table, there is a service offering called "SAP Retail " and Abel was added to the Subscribed by user related list and there is a catalog item called "SAP Access for new hires " added under "Items orderable by subscribers" related list then users whoever subscribed can able to see the catalog item rest of all the users cannot able to see the catalog item.

 

pdurgapavan_0-1757947894276.png

pdurgapavan_1-1757947911303.png

Could anyone please guide me to achieve this.

 

@Dr Atul G- LNG 

@Ankur Bawiskar 

 

1 ACCEPTED SOLUTION

@pdurgapavan 

Logic I shared should work fine, provided you are using correct table and fields to query and grab the value

var catItemSysId = '6117697c1b803a1096064152b24bcba2'; // give sysId of your catalog item

var offeringArr = [];

var gr = new GlideRecord("service_subscribe_sys_user");
gr.addQuery("sys_user", user_id);
gr.query();
while (gr.next()) {
    offeringArr.push(gr.getValue('service_offering'));
}

gs.info('Offering arr is' + offeringArr.toString());

var rec = new GlideRecord('sc_cat_item_subscribe_mtom');
rec.addQuery('sc_cat_item', catItemSysId);
rec.addQuery('service_offering', 'IN', offeringArr.toString());
rec.query();
if (rec.hasNext()) {
    answer = true;
} else {
    answer = false;
}

Try running this in background script and pass user sysId

var catItemSysId = '6117697c1b803a1096064152b24bcba2'; // give sysId of your catalog item

var offeringArr = [];

var gr = new GlideRecord("service_subscribe_sys_user");
gr.addQuery("sys_user", 'hardCodeSysId');
gr.query();
while (gr.next()) {
    offeringArr.push(gr.getValue('service_offering'));
}

gs.info('Offering arr is' + offeringArr.toString());

var rec = new GlideRecord('sc_cat_item_subscribe_mtom');
rec.addQuery('sc_cat_item', catItemSysId);
rec.addQuery('service_offering', 'IN', offeringArr.toString());
rec.query();
gs.info('row count' + rec.getRowCount() + ' encoded query is' + rec.getEncodedQuery());

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

7 REPLIES 7

RaghavSh
Kilo Patron

Did you tried creating user criteria for your catalog item?

This will have to be dine through user criteria, create a user criteria and attach it to your catalog item under "available for".

 

For details refer: https://www.servicenow.com/community/developer-forum/user-criteria-on-catalog-item/m-p/2834070  You can write the script based on your tables


Raghav
MVP 2023
LinkedIn

Ankur Bawiskar
Tera Patron
Tera Patron

@pdurgapavan 

you can use User Criteria and use advanced script and add it to "Available For" related list under your catalog item.

You will have to create separate user criteria for all your catalog items and hard-code the catalog item sysId

Something like this

Note: I believe you can add correct table name and respective table fields in below script and you can enhance it further. Also you cannot get the catalog item sysId dynamically and it has to be static always

var catItemSysId = 'sysId'; // give sysId of your catalog item

var offeringArr = [];

var gr = new GlideRecord("subscribedByUserTable");
gr.addQuery("userField", user_id);
gr.query();
while (gr.next()) {
    offeringArr.push(gr.getValue('offeringField'));
}

var rec = new GlideRecord('itemsOrderableBySubscriberTable');
rec.addQuery('catItemField', catItemSysId);
rec.addQuery('offeringField', 'IN', offeringArr.toString());
rec.setLimit(1);
rec.query();
answer = rec.hasNext();

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

@pdurgapavan 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi  @Ankur Bawiskar ,

 

I tried with the below script and it's not working.

 

/** Scripted User Criteria is not cached, and evaluated everytime, so performance is dependent on the script.
* Populate `answer` with true/false or evaluate to true/false
* The script is evaluated in the scope the user criteria is defined
* Don't use `current` in the script or populate the variable
* Don't use `gs.getUser()` or  `gs.getUserID()`,
* instead use `user_id` which contains the user sys_id against whom the evaluation is happening.
*/

var catItemSysId = '6117697c1b803a1096064152b24bcba2'; // give sysId of your catalog item

var offeringArr = [];

var gr = new GlideRecord("service_subscribe_sys_user");
gr.addQuery("sys_user", user_id);
gr.query();
while (gr.next()) {
    gs.log('hi');
    gs.log(offeringArr.push(gr.getValue('service_offering')));
}

var rec = new GlideRecord('sc_cat_item_subscribe_mtom');
rec.addQuery('sc_cat_item', catItemSysId);
rec.addQuery('service_offering', 'IN', offeringArr.toString());
rec.query();
if(rec.hasNext()){
    answer = true;
}
else{
    answer = false;
}