- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-01-2015 01:53 AM
Good day!
I'm a newbie in ServiceNow. I would like to inquire an error that I'm having right now on Line 6 while creating a business rule.
function onBefore(current, previous) {
var myArray = new Array("u_field_1", "u_field_2","u_field_3", "u_field_4", "u_field_5")
var fields = current.getFields();
var CompletionRate = 0;
myArray.forEach(function(entry){
for (var i = 0; i < fields.size(); i++) {
var field = fields.get(i);
if(field.getName == entry){
if(field.getDisplayValue == 'Completed'){
CompletionRate = CompletionRate + 1;
}
break;
}
}
});
current.completion_rate = ((CompletionRate / 30) * 100) + "%"
}
What I'm trying to do is this. I have declared a variable myArray and stored all the fields that I want to check in my table. Then I want to use (I'm not sure if what I'm trying to do is correct because I haven't used this function yet) the current.getFields(); function to store all the fields in the variable fields.Then, I'm planning to use the forEach function to loop all the fields name that I have stored in my array myArray. Inside that forEach statement, I have created a for loop to check and look if the field name in myArray variable exists in the fields variable that I declared. If it finds the same field, it then checks the value of that field if it is Completed. It then increments the variable that I created CompletionRate.
But unfortunately, I am unable to test the other code that I have if the loop works (I think the logic will work) because I'm having some problems in Line 6. I do have a long method for this. I create many Client Scripts and then use the onChange to immediately compute the CompletionRate variable. But my concern is what if for example you have something around 40 to 50 fields that you want to check.
If there are some alternative method to do this, please do suggest.
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-02-2015 01:07 AM
Hello Jan Raphael,
I suggest you to use the 'getValue' method instead of fetching all fields. This method returns null if your field is empty, or the field does not exist. After modification you Business Rule will be look as:
function onBefore(current, previous) {
var myArray = ["u_field_1", "u_field_2","u_field_3", "u_field_4", "u_field_5", "sys_created_on", "short_description"];
var CompletionRate = 0;
for (var i = 0; i < myArray.length; i++) {
var value = current.getValue( myArray[i] );
if(value && value == 'Completed') {
CompletionRate = CompletionRate + 1;
}
}
current.completion_rate = ((CompletionRate / 30) * 100) + "%";
}
Hope it helps. Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-02-2015 01:07 AM
Hello Jan Raphael,
I suggest you to use the 'getValue' method instead of fetching all fields. This method returns null if your field is empty, or the field does not exist. After modification you Business Rule will be look as:
function onBefore(current, previous) {
var myArray = ["u_field_1", "u_field_2","u_field_3", "u_field_4", "u_field_5", "sys_created_on", "short_description"];
var CompletionRate = 0;
for (var i = 0; i < myArray.length; i++) {
var value = current.getValue( myArray[i] );
if(value && value == 'Completed') {
CompletionRate = CompletionRate + 1;
}
}
current.completion_rate = ((CompletionRate / 30) * 100) + "%";
}
Hope it helps. Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-02-2015 01:44 AM
Hi Epam,
Simple yet amazing. My mind is filled up with complicated stuff and I never look back at the most simplest way to achieve my goal.
Thanks for the tip and thumbs up for you answer!