- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-21-2022 01:25 PM
The below workflow Run Script is using GlideAggregate to count how many RITMs are included in an REQ and adds that number to the correlation_id field of the REQ. I am trying to update it so that it only adds to the correlation_id field if specific catalog items are requested while ignoring others on the same REQ. I tried a number of things including the part I commented out which was an attempt to only count specific catalog item sys_ids, but no luck.
Any suggestions?
var agg = new GlideAggregate('sc_req_item');
agg.addQuery('request', current.sys_id);
//agg.addQuery('sys_id', '566ff7371ba2601064c8a686624bcb98');
agg.addAggregate("COUNT");
agg.query();
if(agg.next()){
var counter = agg.getAggregate("COUNT");
var req_update = new GlideRecord('sc_request');
req_update.get(current.sys_id);
req_update.correlation_id = counter;
req_update.update();
}
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2022 01:15 AM
In that case the code should be something like:
var agg = new GlideAggregate("sc_req_item");
// Select Requested Items of the current Request
agg.addQuery("request", current.getUniqueValue());
// Select only those Requested Items that were created from Catalog Items that have a model associated with it
// It may be necessary to further qualify the selection here, but specifying model category (so that only software models are included)?
agg.addNotNullQuery("cat_item.model")
agg.addAggregate("COUNT");
agg.query();
if (agg.next()) {
var counter = agg.getAggregate("COUNT");
current.correlation_id = counter;
}
Also because the Business Rule runs on the same table as the one modified, this must be a Before Business Rule, not an After Business Rule.
'Cause we know the rules:
- No updating current in Business Rules; that means not only calling current.update(), but also loading the current record and updating it.
- If the current record is modified, it has to be a Before Business Rule
- If a different record is modified, it has to be an After Business Rule, unless what is modified is not loaded into the form in which case it can also be an Async Business Rule to allow faster form load times.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-21-2022 05:03 PM
I mean I assumed 566ff7371ba2601064c8a686624bcb98 is a Catalog Item unique ID, but maybe it is a Configuration Item unique ID (sys_id)?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2022 09:01 AM
Hello Janos,
566ff7371ba2601064c8a686624bcb98 is the sys_ID of the catalog item for SnagIT software.
Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2022 12:46 PM
If that is the sys_id of a Catalog Item included multiple times in a request (b.t.w. do you want the count for the current request only, or any request) and current is a Requested Item that is part of a Request that also contains the SnagIT Catalog Item, the code should update that field with at least 1. If it does not, then something else must be the problem, like the Requested Items do not in fact belong to the same request, or the Business Rule is scheduled to run at the wrong time, etc. But the code has been tested (of course with proper sys_id). It also seems strange to me, especially now that you confirm that the sys_id is indeed that of the SnagIT Catalog Item, that a software would be included multiple times into the same request, except if this is perhaps a delegated request experience instance?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2022 03:49 PM
That is the correct sys_ID for the SnagIT catalog item.
Yes, I only want to count items (ex SnagIT) on the current request only, not all requests. The reason someone may request 2 SnagITs on a single REQ is because user can request the software for multiple people on one REQ.
Not sure why your code doesn't populate the CorrID. You may be right that a Biz Rule may be an issue. There's an Out-of-Box rule called "Can request be sourced" that looks for catalog items that have a Model in the catalog item record. That's really what I'm trying to do. Only count items (like SnagIT) that have a model. The problem with the code I initially provided is if you request SnagIT (which has a model) and an iphone (no model) the CorrID is 2 when it should be 1.
Below is the rule "Can request be sourced". We don't have plugin com.sn_hamp installed so much of it is irrelevant. I tried striping that code out and making changes to gr.addQuery but still can't get this to work.
Thanks.
sourceable(current);
function sourceable(record) {
var C_STOCK_ORDER_CAT_SYS_ID = '4109aa5fdb22001015a8ffefbf961984';
var gr = new GlideRecord("sc_req_item");
gr.addQuery("request", record.sys_id);
//A OR (B AND C) not supported
//Hence going for (A OR B) AND (A OR C)
var qc = gr.addNotNullQuery("cat_item.model");
if (GlidePluginManager.isActive('com.sn_hamp')) {
qc.addOrCondition("cat_item.sys_id", C_STOCK_ORDER_CAT_SYS_ID);
qc.addOrCondition('variables.dbccd3f2b7621010189e22b5de11a90e', '!=', '');//expecting this catalog item only if HAMP is active, hence not checking if HAMP active or not
// need to enhance code
var qc1 = gr.addNotNullQuery("cat_item.model");
qc1.addOrCondition("variables.6189629fdb22001015a8ffefbf96197f", "!=", "");//Checking if model variable is not null
qc1.addOrCondition('variables.dbccd3f2b7621010189e22b5de11a90e', '!=', '');
}
gr.setLimit(1);
gr.query();
if (gr.hasNext()) {
record.sourceable = true;
record.update();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2022 04:08 PM
Now that I think of it, it is almost obvious. The issue might be that when a Request is submitted, 1st the Requested Items are created than the parent Request. That means when the Requested Items are running the Business Rule, the Request does not yet exist. So just move the Business Rule to run on the Request table after insert.
As a clarification, do you want to count any Catalog Items that have a model, or only SnagIT?
The code needs to change if moved to sc_request and will need additional changes if any Catalog Item with model should be counted.