- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-22-2023 12:50 PM
I have 5 choice fields on a form,
choice field 1 = label of Yes and a value of 3
label of NO and Value of 0
choice field 2 = label of Yes and a value of 3
label of NO and Value of 0
choice field 3 = label of Yes and a value of 3
label of NO and Value of 0
choice field 4 = label of Yes and a value of 3
label of NO and Value of 0
I also have a field Integer that will hold the total
I want to capture the yes and no of each choice and add the sum to the total field. Here is my business rule i am using , after update
var fieldstoTotal = ['current.choice_field1', 'current.choice_field2', 'current.choice_field3', 'current.choice_field4'];
var total = 0;
fieldstoTotal.forEach(function(fieldName) {
var fieldValue = parseFloat(current.getValue(fieldName)) || 0;
total += fieldValue;
});
current.setValue('survey_score', total);
for some reason im not sure why its coming back undefined for the fields.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-26-2023 07:56 AM
@ServNowDev : To replicate the scenario, I created two choice fields, similar to your use case, and I executed the script that I posted earlier, which is working. Please refer to the below screenshots.
For one of the Incident record, I have updated the choice values as per the below.
Executed the same script from BG and I got the desired output. Please refer below screenshot.
Please make sure that your code is correct as per the sample I provided and test it out.
Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-22-2023 01:13 PM - edited 12-22-2023 03:57 PM
Have you tried using:
var fieldValue = parseFloat(current[fieldName].value) || 0;
or just
var fieldValue = parseFloat(current[fieldName]) || 0;
?
(According to your request i assume you have a label and value field inside each object. If so then the first code should work)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-22-2023 01:48 PM - edited 12-22-2023 02:16 PM
@ServNowDev : It's not required to mention current in your array, as you are getting the value of each field in the forEach loop. Could you try with the below code, and please make sure that all your field names are correct in the script?
Note: Please change your logic to before update business rule, so the field gets updated at the same time without any additional .update() command.
var fieldstoTotal = ['choice_field1', 'choice_field2', 'choice_field3', 'choice_field4'];
var total = 0;
fieldstoTotal.forEach(function(fieldName) {
var fieldValue = parseFloat(current.getValue(fieldName)) || 0;
total += fieldValue;
});
current.setValue('survey_score', total);
@ServNowDev Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-22-2023 03:54 PM
The 'fieldstoTotal' array on which he is iterating the forEach loop is just an array holding strings of the actual field names, so this wont return any values by itself, besides the string. But the field variable can be used to get the desired field value from the current object using it as an index like: current[field]
You could instead iterate over the current object as an alternative, but this is a more effective/best practise method.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-22-2023 06:33 PM
That’s is what I mentioned that he is actually getting the values from for each loop getValue and not needed to mention current again in the field array.
I tested the code I mentioned and it’s working as per his ask. Thanks!