- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-12-2016 09:17 AM
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.
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:
Script Include
Name: FastTrack_Quasar_getScore
Client callable: true
Script:
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):
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-15-2016 10:25 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-12-2016 09:39 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-15-2016 08:51 AM
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:
Client script (x5 for each field but using same script):
Thank you!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-15-2016 09:01 AM
Why do you have g_form functions in script include ? The suggestion was to create a client side function in a onload client script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-15-2016 10:25 AM
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