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

Steve McCarty
Mega Guru

Kamile,



Do you need to have the weighted values on a separate table, or could you put the values of the choices in the choice lists?   In any drop down there is a separate label and value for each choice.   So if the labels are Low, Medium, and High you could then set the values to 1, 3, and 5 respectively.   Then you can use some simple client scripts to do all the calculations.



First create an client callable script include that defines a function for calculating the scores like this:



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 create onChange scripts for each drop down box that simply call the calculateScore() function.   If the script include has the same name as the function, you don't have to do anything in your clients scripts except put the one call to calculateScore().   This will make the page very responsive since you aren't waiting for any GlideAjax calls, and the score will be updated as the user changes the boxes.



-Steve


Hi Stephen,



Yes, I was using a separate table and storing values in it. I have now changed the fields to choice and added options of High, Medium and Low with values of 5, 3 and 1 respectively for each. I also created the new script include and onChange client scripts and disabled all previously created scripts. However, it does not seem to work. Am I missing something?



New script include:


find_real_file.png



Client script (x5 for each field but using same script):


find_real_file.png



Thank you!


Why do you have g_form functions in script include ? The suggestion was to create a client side function in a onload client script.


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