Filling up percent score field based on checkboxes
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2024 06:45 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2024 07:06 AM - edited 01-23-2024 07:07 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2024 12:35 AM
Just wanted to follow up to see if this did the trick or if I misunderstood the requirement at all?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2024 01:50 AM
Unfortunately, this did not work but it provided a vision for me.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2024 01:16 AM
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