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.

Condition to calculate score from multiple choice answers

Sofija
Giga Expert

Hi All,

I started creating code for calculating a score to be used for prioritisation of request items based on an answer to 5 choice questions (as shown below). I created a separate table where I determined what score does each answer amount to (here Low is 1, Medium is 3, High is 5). Therefore, for example, a selection of Deadline=Low, Losses=Low, Financial risk=Medium, Reputational risk=High, and Regulatory risk=Medium would results to a score of 1+1+3+5+3=13. However, instead of the adding up the score so far I only got to each field pushing its value to the Overall Score field.

find_real_file.png

Note: The choices in each field refer to the u_req_item_fast_track_request_criteria table based on their type.

As part of getting to this I wrote 1 Business rule, 1 Script Include and 5 Client Scripts, but I know I am missing a condition somewhere to add the scores. Scripts are outlined below.

Business rule

Table: Requested item [sc_req_item]

When: before insert

Advanced: true

Script:

find_real_file.png

Script Include

Name: FastTrack_Quasar_getScore

Client callable: true

Script:

find_real_file.png

Client Scripts

Table: Requested item [sc_req_item]

Type: onChange

Field names (there are 5 separate scripts for each field): Deadline, Losses to date, Financial risk, Reputational risk, Regulatory risk

Script (same for all 5 fields):

find_real_file.png

Could someone be able to help me and guide where and what kind of script I should enter in order to achieve the calculation of Overall Score? I know it should be something like current.u_quasar_overall_score = current.dline + current.loss + current.fnc + current.rep + current.reg; but because it is three dimensional and the scores for dline, loss, fnc, rep, reg are on a different table I feel I am getting lost on how to calculate them rather than push as I am doing with the current scripts.

I would really appreciate everyone's help and I will mark answers as helpful, correct and like based on their relevance!

Kamile

1 ACCEPTED SOLUTION

Kamille,



It looks like I steered you a little wrong.   You can't put that in a script include.   You have to create an onLoad client script for that table that defines the function.   The onLoad client script should be:



function onLoad() {


  //Type appropriate comment here, and begin script below


  calculateScore();


}



function calculateScore()  


{


var score = 0;  


      score += g_form.getValue('u_quasar_deadline');  


      score += g_form.getValue('u_quasar_losses_to_date');  


      score += g_form.getValue('u_quasar_financial_risk');  


      score += g_form.getValue('u_quasar_reputational_risk');  


      score += g_form.getValue('u_quasar_regulatory_risk');  


      g_form.setValue('u_quasar_overall_score', score);  


}


Then each of the client scripts only needs the line:



calculateScore();



If the script doesn't recognize that the values are numbers you may have to wrap each of the lines in the Number() function like this:



score += Number(g_form.getValue('u_quasar_deadline'));



See if that works for you.



-Steve


View solution in original post

5 REPLIES 5

Thank you - this worked just fine and I didn't even need the onChange client scripts.