Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

Michael Fry1
Kilo Patron

There is out of box BR: Recreate Requested Allocations, that looks like it doesn't run when Allocated, but maybe you can use the script to force recalcs?

var rp = new ResourcePlan();
rp.get('37313f471383e34084b3b7a66144b00c');
rp.reCreateRequestedAllocations();

I'll give that one a shot, but I'm doubting it.   I can get the requested allocations to update just fine.  Its the resource_allocations that I can't get to budge.

Harsha Lanka
ServiceNow Employee
ServiceNow Employee

Hi Robert,

can you change your 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.

Let me know whether it has corrected both resource_allocation.allocated_cost and resourceplan.allocated_cost?  Try this in clone instance.

Thanks,
Harsha Lanka

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.