- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-03-2023 11:12 AM
Hello All,
I have a requirement to fetch the details from a field.
Table 'Task' records the time (time_worked) a user has worked on the CASE (CS0132374) and RITM (RITM0013737) as below:
The time worked field gets updated every time a user makes update on RITM/CASE (the time gets added automatically). I want to store the both the time a user has worked on RITM and CASE in two different variable so I can use those variables to process ahead as per requirement. I tried creating a BR with table as task. I am getting the value for Case every time using 'current' but not getting the value of RITM here. Unaware of what method to use so that I can get the value of both records in two different variables at the same time.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-06-2023 02:56 AM - edited 11-06-2023 03:03 AM
Based on what you wrote showed, I would create two business rules, one after on Case and an additional after on Requested Item;
The code would be:
(function executeRule (current, previous) {
var caseUniqueValue = current.getUniqueValue(); // (Only!) In the case BR
var caseUniqueValue = current.request.parent; // (Only!) In the requested item BR
var totalTimeWorked = new GlideDuration(getTotalTimeWorked(caseUniqueValue));
// Use totalTimeWorked to update the entitlement
function getTotalTimeWorked (caseUniqueValue) {
var $ga = new GlideAggregate('task');
$ga.addAggregate('SUM', 'time_worked');
// Only bother with records that have time recorded
$ga.addNotNullQuery('time_worked')
// Include the (parent) case
$ga.addQuery('sys_id', caseUniqueValue)
// Also include any requested items the (parent) case may have
.addOrCondition('ref_sc_req_item.request.parent', caseUniqueValue);
$ga.setGroup(false);
$ga._query();
if ($ga._next())
return $ga.getAggregate('SUM', 'time_worked');
else
return '0';
}
})(current, previous);
Note that you would keep only one of the caseUniqueValue variable declarations only in both Business Rules - the one correct for the given table, of course.
Note also that this is lacking the code that updates the entitlement.
The code makes use of the fact that field time_worked is defined on table Task, the parent both of Case and Requested Item.
Thus the select can be executed against it and it will select both the Case and any Requested Items it may contain.
Also to make it optimal, it does an aggregation in database, returning the total already, not needing any resource intensive in-script processing.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-06-2023 06:24 AM
@-O- Hi, I have created two BR, one for case and another for RITM.
The first line for case table (var caseUniqueValue = current.getUniqueValue) gives me undefined result in log whereas the second line for RITM table( var caseUniqueValue = current.request.parent;) shows empty log meaning the script didnt even ran as no other logs where printed when user tried to make update on RITM.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-06-2023 02:16 AM
Hello -O-,
Thanks for your prompt response.
Relationship is something new to me. Below is what I found in the table as suggested by you:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-06-2023 02:56 AM - edited 11-06-2023 03:03 AM
Based on what you wrote showed, I would create two business rules, one after on Case and an additional after on Requested Item;
The code would be:
(function executeRule (current, previous) {
var caseUniqueValue = current.getUniqueValue(); // (Only!) In the case BR
var caseUniqueValue = current.request.parent; // (Only!) In the requested item BR
var totalTimeWorked = new GlideDuration(getTotalTimeWorked(caseUniqueValue));
// Use totalTimeWorked to update the entitlement
function getTotalTimeWorked (caseUniqueValue) {
var $ga = new GlideAggregate('task');
$ga.addAggregate('SUM', 'time_worked');
// Only bother with records that have time recorded
$ga.addNotNullQuery('time_worked')
// Include the (parent) case
$ga.addQuery('sys_id', caseUniqueValue)
// Also include any requested items the (parent) case may have
.addOrCondition('ref_sc_req_item.request.parent', caseUniqueValue);
$ga.setGroup(false);
$ga._query();
if ($ga._next())
return $ga.getAggregate('SUM', 'time_worked');
else
return '0';
}
})(current, previous);
Note that you would keep only one of the caseUniqueValue variable declarations only in both Business Rules - the one correct for the given table, of course.
Note also that this is lacking the code that updates the entitlement.
The code makes use of the fact that field time_worked is defined on table Task, the parent both of Case and Requested Item.
Thus the select can be executed against it and it will select both the Case and any Requested Items it may contain.
Also to make it optimal, it does an aggregation in database, returning the total already, not needing any resource intensive in-script processing.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-06-2023 07:55 AM
I got it why the script wasn't working.
new KNVLCSMUtils().isCaseRelatedToRequest(current) == true
The above line in condition was restricting the time monitoring on RITM.
Thanks @-O-
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-06-2023 08:18 AM - edited 11-06-2023 08:20 AM
You're welcome 🙂
And I appreciate you marking the correct answer.