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.

Filling up percent score field based on checkboxes

Mouktik_B
Tera Contributor

I have 5 checkboxes (true/false) fields . if they are true the percent score field increases by 20% . 5 boxes leads to 100% and unchecking one reduces the percentage by 20%. 
Need the percent score to populate actual percentage based on the checkboxes done manually. 

5 REPLIES 5

Joe B2
Giga Guru

Lots of different ways to do this. Assuming this doesn't need to be done client side and it's OK to do it server side, I would probably do the following:

 

  • Create a Business Rule on the table which triggers when one of your 5 fields are updated.
  • Put the following in the script field:

 

var fields = ['checkBox1', 'checkBox2', 'checkBox3', 'checkBox4', 'checkBox5']; //Change these to the names of your checkbox fields
        var trueCount = 0;

        for (var i = 0; i < fields.length; i++) {
            var fieldName = fields[i];
            if (current[fieldName].toString() == 'true') {
                trueCount++;
            }
        }

        var percent = (trueCount / fields.length) * 100;
        current.percentageField = percent; //Change this field to your percentage field name

 

Untested but should do the trick.

Just wanted to follow up to see if this did the trick or if I misunderstood the requirement at all?

Mouktik_B
Tera Contributor

Unfortunately, this did not work but it provided a vision for me. 

Rajdeep Ganguly
Mega Guru


Sure, you can achieve this by using a client script in ServiceNow. Here's a sample script that you can use:

javascript
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}

var checkbox1 = g_form.getValue('checkbox1');
var checkbox2 = g_form.getValue('checkbox2');
var checkbox3 = g_form.getValue('checkbox3');
var checkbox4 = g_form.getValue('checkbox4');
var checkbox5 = g_form.getValue('checkbox5');

var score = 0;

if (checkbox1 == 'true') {
score += 20;
}
if (checkbox2 == 'true') {
score += 20;
}
if (checkbox3 == 'true') {
score += 20;
}
if (checkbox4 == 'true') {
score += 20;
}
if (checkbox5 == 'true') {
score += 20;
}

g_form.setValue('percent_score', score);
}


Here are the steps to implement this:

1. Navigate to System Definition > Client Scripts in ServiceNow.
2. Click on New to create a new client script.
3. Fill in the fields:
- Name: Enter a unique name.
- Table: Select the table where your fields are located.
- Type: Select onChange.
- Field name: Select one of the checkbox fields.
- Script: Paste the above script.
4. Repeat steps 2-3 for each checkbox field.
5. Click on Submit.

This script will run every time a checkbox field is changed, calculate the new score, and update the 'percent_score' field. Make sure to replace 'checkbox1', 'checkbox2', etc. with the actual names of your checkbox fields, and 'percent_score' with the actual name of your score field.


nowKB.com

For asking ServiceNow-related questions try this :
For a better and more optimistic result, please visit this website. It uses a Chat Generative Pre-Trained Transformer ( GPT ) technology for solving ServiceNow-related issues.
Link - https://nowgpt.ai/

For the ServiceNow Certified System Administrator exams try this :
https://www.udemy.com/course/servicenow-csa-admin-certification-exam-2023/?couponCode=NOW-DEVELOPER