Fetching two current object details

swapnil15
Tera Contributor

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: 

swapnil15_0-1699034819792.png

 

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. 

 

1 ACCEPTED SOLUTION

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.

View solution in original post

17 REPLIES 17

swapnil15
Tera Contributor

@-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.

swapnil15
Tera Contributor

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:

swapnil15_0-1699265800066.png

 

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.

swapnil15
Tera Contributor

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- 

You're welcome 🙂

And I appreciate you marking the correct answer.