Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Recalculating Resource Plan Allocated Cost since rate card correction

Uncle Rob
Kilo Patron

SCENARIO:
Resource plan created, generating requested_allocations.  resource_plan.planned_cost calculates correctly since its using the group_resource rate.

Because of the FiretrUCKING Labor Rate Card table swapping bug, hundreds of allocations have been created with the wrong resource_allocation.allocated_cost.  That means resource_plan.allocated_cost is also borked.

Deconstructing the ResourcePlan script include I've found functions that allow me to recalculate planned cost, however no functions appear to recalculate and update a resource_allocation's allocated_cost, then recalculate the resource_plan.allocated_cost.  

Anyone have any insights?

Here's a script I've been working with.  updatePlanned() works, but updateAllocated() does nothing.

function updatePlanned(req) {
	var resourcePlan = new ResourcePlan();
	resourcePlan.get(req.getValue('resource_plan'));
	resourcePlan.updatePlannedHoursAndCost(req.getTableName());
}

function updateAllocated(alloc){
  gs.print('in updateAllocated');
	var allocation = new ResourceAllocation(alloc);
	allocation.calculateAllocatedCost();
	var resourcePlan = new ResourcePlan();
	//gs.print(alloc.getValue('resource_plan'));
	resourcePlan.get(alloc.getValue('resource_plan'));
	//resourcePlan.updateAllocatedCost();
	resourcePlan.updateAllocatedHoursAndCost();
}

var rp = new GlideRecord('resource_plan'); 
rp.get('37313f471383e34084b3b7a66144b00c'); //my sample Resource Plan
gs.print(rp.short_description);

var rpreqs = new GlideRecord('requested_allocation');
rpreqs.addQuery('resource_plan',rp.sys_id);
rpreqs.query();
while (rpreqs.next()){
  updatePlanned(rpreqs);
}

var rpallocs = new GlideRecord('resource_allocation');
rpallocs.addQuery('resource_plan',rp.sys_id);
rpallocs.query();
while (rpallocs.next()){
  gs.print('Launch updateAllocated');
  updateAllocated(rpallocs);
}
1 ACCEPTED SOLUTION

Harsha Lanka
ServiceNow Employee
ServiceNow Employee

Hi Robert,

Can you change the below function from

function updateAllocated(alloc){
  gs.print('in updateAllocated');
	var allocation = new ResourceAllocation(alloc);
	allocation.calculateAllocatedCost();
	var resourcePlan = new ResourcePlan();
	//gs.print(alloc.getValue('resource_plan'));
	resourcePlan.get(alloc.getValue('resource_plan'));
	//resourcePlan.updateAllocatedCost();
	resourcePlan.updateAllocatedHoursAndCost();
}

to

function updateAllocated(alloc){
	var allocation = new ResourceAllocation(alloc);
	allocation.updateAllocatedHoursAndCost();
	var resourcePlan = new ResourcePlan();
	resourcePlan.get(alloc.getValue('resource_plan'));
	resourcePlan.updateAllocatedHoursAndCost();
}

and execute this. Try executing this in some temp instance.

View solution in original post

8 REPLIES 8

Hey Harsha,
Isn't that the exact same code, just with the commented lines knocked out?

 

EDIT:  Wait!  Nope, I'm just blind.  Thanks for the tip.  I'll try this!

No. You are calling 

allocation.calculateAllocatedCost();

If you see the actual code, it just sets allocated cost but doesn't update.

I am calling

allocation.updateAllocatedHoursAndCost();

which actually updates hours and costs as well.

DUDE!  Thank you!find_real_file.png

So grateful for this, Harsha.
If you have the time, could you explain why its got to run ResourceAllocation.updateAllocationHoursAndCost as well as ResourcePlan.updateAllocatedHoursAndCost?