Auto calculate assessment question

Joe Weisiger
Giga Expert

Hello,

I have an assessment question setup that I want to have automatically calculated. 

On the metric, I have the following fields set:

- Name: Project Capital Spend

- Method: Default answer from script

- Data type: Number

- Dependent plugin: Core

At the end of the script I have the setValue to the metric, but for some reason there is no answer to the question when the assessment is taken.

Any help is appreciated!

 

 

// Capital spend as of the end of the warranty period (2 months after its in-service date)
function calculateScore() {
  var plannedInServiceDate = g_form.getValue('u_planned_in_service_date');
  var plannedDate = new GlideDateTime(plannedInServiceDate);

  // Calculate the warranty end date (2 months after the planned in-service date)
  var warrantyEndDate = new GlideDateTime(plannedDate);
  warrantyEndDate.addMonthsLocalTime(2);

  var capitalOutlay = parseFloat(g_form.getValue('capital_outlay')) || 0;

  var totalExpectedCapitalSpend = 0;

  // Query cost plans related to this project
  // var costPlans = getCostPlans(/* Query parameters to get relevant cost plans for this project */);

  // Calculate the total expected Capital spend from relevant cost plans
  for (var i = 0; i < costPlans.length; i++) {
    var fiscalPeriodStart = new GlideDateTime(costPlans[i].start_fiscal_period);
    var fiscalPeriodEnd = new GlideDateTime(costPlans[i].end_fiscal_period);
    var plannedCost = parseFloat(costPlans[i].cost_local_currency) || 0;
    var resourceType = costPlans[i].resource_type || '';

    if (resourceType.toLowerCase().includes('capital') && fiscalPeriodStart <= warrantyEndDate) {
      // If the resource type contains "capital" and the fiscal period of the cost plan is on or before the end of the warranty period,
      // consider planned cost as the actual capital spend
      totalExpectedCapitalSpend += plannedCost;
    } else if (resourceType.toLowerCase().includes('capital') && fiscalPeriodStart <= plannedDate) {
      // If the resource type contains "capital" and the fiscal period of the cost plan is after the planned in-service date but before the warranty end,
      // prorate the capital expenditure
      var warrantyPeriod = warrantyEndDate - plannedDate;
      var totalPeriod = fiscalPeriodEnd - plannedDate;
      var proratedPlannedCost = (plannedCost * warrantyPeriod.getNumericValue()) / totalPeriod.getNumericValue();
      totalExpectedCapitalSpend += proratedPlannedCost;
    }
  }
}
var gr = new GlideRecord('dmn_demand');
if (gr.get(primary)) {
	
  // Set the score based on the total expected Capital spend
  if (gr.totalExpectedCapitalSpend === 0) {
    scaled_result = 0;
  } else if (gr.totalExpectedCapitalSpend < 250000) {
    scaled_result =  1;
  } else if (gr.totalExpectedCapitalSpend < 750000) {
    scaled_result =  2;
  } else if (gr.totalExpectedCapitalSpend < 1500000) {
    scaled_result =  3;
  } else if (gr.totalExpectedCapitalSpend < 2000000) {
    scaled_result =  4;
  } else {
    scaled_result =  5;
  }
}

actual_result = gr.totalExpectedCapitalSpend;

 

 

 Thank you,

Joe

0 REPLIES 0