Context- CRM system, front-end form.
Requirement- Within a form users have to give a rating to a section based on user inputs, each rating has different weighting. The rating is stored in an option set field. I have used JavaScript to calculate the average score of these rating fields. Also the weighting is stored in a separate option set field.
The option sets do not simply give me the score of say "1" they give me the value the option set is in the database " 778180000" from this I need to retrieve the score. Based on the option set value chosen I can set the score for example Option Set Chosen: "1- Poor performance" database value = 778180000, then from this I know I can set the score variable for section A to (1).
These percentage values could potentially change per business requirements. For example in the following year maybe Section A will increase to 50% AND Section B will change t0 20% this needs to be taken into account.
I am looking for code improvements. An how I can use functions / methods and objects. How could I implement some error handling?
*Beginner to JS.
function CalculateAverageScore(executionContext) {
var formContext = executionContext.getFormContext();
var ScoreOptionA = formContext.getAttribute("dp_testweightedaverage");
var PercentageOptionA = formContext.getAttribute("dp_percentagevalue");
var ScoreOptionB = formContext.getAttribute("dp_average2");
var PercentageOptionB = formContext.getAttribute("dp_percentagevalue2");
var ScoreOptionC = formContext.getAttribute("dp_pickascorefirmwide");
var PercentageOptionC = formContext.getAttribute("dp_percentagevaluefirmwide");
// Define Scoring & Percentages
var scoreMap = {
778180000: 1,
778180001: 2,
778180002: 3,
778180003: 4,
778180004: 5
};
var percentageMap = {
778180000: 0.10,
778180001: 0.20,
778180002: 0.30,
778180003: 0.40,
778180004: 0.50
};
var SectionAScore, SectionAPercentage, SectionBScore, SectionBPercentage, SectionCScore, SectionCPercentage;
// Section A Score & Percentages
if (ScoreOptionA != null && PercentageOptionA != null) {
var ScoreValueA = ScoreOptionA.getValue();
var valuePercentageA = PercentageOptionA.getValue();
SectionAScore = scoreMap[ScoreValueA];
SectionAPercentage = percentageMap[valuePercentageA];
}
//Section B Set Score & Percentages
if (ScoreOptionB != null && PercentageOptionB != null) {
var ScoreValueB = ScoreOptionB.getValue();
var valuePercentageB = PercentageOptionB.getValue();
SectionBScore = scoreMap[ScoreValueB];
SectionBPercentage = percentageMap[valuePercentageB];
}
// Section C Score & Percentages
if (ScoreOptionC != null && PercentageOptionC != null) {
var ScoreValueC = ScoreOptionC.getValue();
var valuePercentageC = PercentageOptionC.getValue();
SectionCScore = scoreMap[ScoreValueC];
SectionCPercentage = percentageMap[valuePercentageC];
}
// Calculations
var Section1Score = (SectionAScore * SectionAPercentage);
var Section2Score = (SectionBScore * SectionBPercentage);
var Section3Score = (SectionCScore * SectionCPercentage);
var averagescore = (Section1Score + Section2Score + Section3Score);
// Set Average Score Value
formContext.getAttribute("dp_decimal").setValue(averagescore);
}