kevin-kilroy
3/21/2020 - 11:32 AM

Calculate Score

Requirements

Q1: How many total applications are expected to use this service (including your app)?

  • 1
  • 2 (+1)
  • 3+ (+1)

Q2: Has the service been made Generally Available (GA) by the cloud service provider?

  • Yes (+3)
  • No

Q3: Is this service providing a new capability or is it enhancing an existing capability?

  • New Capability (+2)
  • Enhancing Existing Capability

Q4: How many developers are expected to use this service?

  • 1-10
  • 10-20
  • 20-50 (+1)
  • 50-100 (+1)
  • 100+ (+1)

Q5: Is it expected that this service will increase PROFIT GENERATION?

  • Yes (+1)
  • No
  • Maybe

Solution Design

How the data is represented and stored

  • Each question has its own field of type Choice.
  • Each possible answer to the question is a choice.
  • Each choice value is a number, based on the order of the possible answer - e.g. the first answer has a value of 1.
  • Each question has an associated system property with the score values of each possible answer separated by a comma - e.g. if Yes, No where Yes is a +3 the system property is 3,0
  • The system property name is sa_score_[field_name]. e.g. sa_score_total_applications.

Q1: total_applications

  • Choice list values: 1,2,3
  • System property: 0,1,1

Q2: generally_available_status

  • Choice list values: 1,2
  • System property: 3,0

Q3: service_capability_status

  • Choice list values: 1,2
  • System property: 2,0

Q4: total_developers

  • Choice list values: 1,2,3,4,5
  • System property: 0,0,1,1,1

Q5: profit_generation

  • Choice list values: 1,2,3
  • System property: 1,0,0

Scripting to calculate the score

Overview

  • Every field has an onChange client script that

    • takes the new value of the field that is changes
    • and the current value of the other fields in the score calculation
    • passed those value to a Script Include via GlideAjax
    • GlideAjax returns a score
    • the assessment_score field is updated with the returned value.
  • Each onChange client script relies on the Script Include ServiceAssessmentUtil, which includes two methods:

    • calculateScore is the public method called from the GlideAjax in the client script.
      • For each value passed from the client script, it called the private method _getScore.
      • The result of each call to _getScore is summed in the variable score
      • score is returned
    • _getScore takes a value (value) and a field name (fieldName). - the value of value is a string, so it is changed to an integer with parseInt() - the system property name to be retrieved is calculated based on a standard prefix plus the field name value passed in. - The values from the system property are retrieved, then converted to an array using .split(',') - Since the score values align like 1,2,3...n we need to take the value passed in an subtract 1 from it. The array created from the system property value has a zero-based index. So, when we have a value of 1 we want to get get the corresponding element at array[0], not array[1]. - The value of the element from the array is convert an integer and returned

Testing the logic

This script can be run as a background script to test the logic.

newValue is changed value from the field in the client script.

calculateScore() and _getScore() contain the logic used in the script include.

var newValue = '1'; // score 0
var totalApps = newValue;
var genAvail = '1'; // score: 3
var serviceCap = '2'; // score: 0
var totalDevs = '3'; // score: 1
var profitGen = '3'; // score: 0

var result = calculateScore();
gs.info(result);

function calculateScore() {
  var score = 0;
  // get the scrore for each field based on the value passed it
  var ta = this._getScore(totalApps, 'total_applications');
  var ga = this._getScore(genAvail, 'generally_available_status');
  var sc = this._getScore(serviceCap, 'service_capability_status');
  var td = this._getScore(totalDevs, 'total_developers');
  var pg = this._getScore(profitGen, 'profit_generation');
  // add up all the results
  score = ta + ga + sc + td + pg;
  return score;
}

function _getScore(value, fieldName) {
  /* declare variables
score is the ultimate result will be returned
convert the value passed in to an integer
calculate the property name from the field name passed in
get the system property
since retrieving score from array, you're getting the element of value - 1.
*/
  var score = 0,
    value = parseInt(value, 10),
    propName = 'x_12350_playground.sa_score_' + fieldName,
    propValues = gs.getProperty(propName).split(','),
    getValue = value - 1;
  // get the value from the array and make it an integer
  score = parseInt(propValues[getValue], 10);
  return score;
}