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

Forgot to mention/clarify: when using this solution you don't need variables in which to store the values and you don't need processing as all of that will be taken care of on the database level already.

Also your original intent to have (just) two variables is not recommended.

There is nothing stopping anyone from adding as many requests to a Case as needed, in which case - of course - just two variables (one for the case, another for a requested item) would not be enough.

Some further clarification: if the entitlement that you want to update is the field on Case, than the BR on Case should be different.

And most important it should be before BR, not after BR.

(As the BR rules says:

- the current record is to be updated in before BRs

- other records should be updated in after BRs.)

The initial select will not work, because current is an in-memory state of the record, thus the in-memory value of time_worked of the current Case might be different from the on-disk value of time_worked - which the GlideAggregate (or any GlideRecord) would in fact count/select.

Because of that, in case of the before BR on Case the strategy should be: sum up the total worked time of Requested Items, add current.time_worked to that and update the result in entitlement.

 

swapnil15
Tera Contributor

The entitlement has two fields: Total time worked and Remaining Time. I have to deduct the hours, a user works on RITM and CASE from the remaining time. The case can be seen in the related list tabs of entitlement as below:

swapnil15_0-1699281003943.png

Agree, with the before BR here. But the provided script still doesnt work.

Thanks for your time and response.

Well, I have tried the part that I posted and for sure it worked.

If you could provide the whole BR as you have it, I/the community could provide further help.

@swapnil15 You are writing business rule on "sc_req_item" table and you are doing glide record query to same table. Its not required at all. If you want to verify time worked value for RITM then simple add gs.addInformessage()  as below and try updating the RITM record then you will that message.

 

can you see Time worked field on the RITM form ? check my below post

SANDEEP28_0-1699265876457.png