- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-14-2021 07:19 AM
For a business rule why does current.field = "any string" not actually update the field to that string even if a condition is met.
Here is my script, I have debugged it and it reaches current.u_empty = "false"; but it does not actually set the value to "false" it just leaves it at undefined.
(function executeRule(current, previous /*null when async*/) {
var gr = new GlideRecord("cmdb_ci_business_app");
gr.query();
while(gr.next()) {
var fields = gr.getFields();
for(var i = 0; i < fields.size(); i++) {
var ge = fields.get(i);
if(ge.nil()) {
current.u_empty = "true";
break;
} else {
current.u_empty = "false";
}
}
}
})(current, previous);
Solved! Go to Solution.
- Labels:
-
Personal Developer Instance
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-15-2021 10:14 PM
Hi,
for some field it must be going to if and hence keeping it true
can you try this and check once
(function executeRule(current, previous /*null when async*/) {
var flag = false;
var field;
var gr = new GlideRecord("cmdb_ci_business_app");
//no need for any specific filters here.
gr.query();
while(gr.next()) {
var fields = gr.getFields();
for(var i = 0; i < fields.size() - 1; i++) {
var ge = fields.get(i);
var descriptor = gr.getED();
var fieldName = descriptor.getName();
var fieldValue = gr[fieldName].getDisplayValue();
if(!fieldName.startsWith('sys')){
if(fieldValue.nil()) {
flag = true;
field = fieldName;
break;
} else {
flag = false;
}
}
}
}
if(flag){
gs.info('Field ' + field);
current.setValue('u_new', true);
}
else{
current.setValue('u_new', false);
}
})(current, previous);
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-15-2021 07:04 AM
I changed var descriptor = gr.getED() to var descriptor = ge.getED() and it works it skips all the sys_ fields, but current.setValue("u_new", false); is not changing the value to false. I tried debugging again to see what the issue was but all the fields it iterated through were filled and there was nothing undefined.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-15-2021 07:23 AM
Hi,
If it was already false then it won't have any impact
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-15-2021 07:47 AM
The record was first true because there were empty fields but then when I fill out the fields it does not change back to false.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-15-2021 08:07 AM
Hi,
if you fill out the fields with values it won't go inside the if since it is checking for nil
(function executeRule(current, previous /*null when async*/) {
var gr = new GlideRecord("cmdb_ci_business_app");
//no need for any specific filters here.
gr.query();
while(gr.next()) {
var fields = gr.getFields();
for(var i = 0; i < fields.size() - 1; i++) {
var ge = fields.get(i);
var descriptor = gr.getED();
var fieldName = descriptor.getName();
var fieldValue = gr[fieldName].getDisplayValue();
if(!fieldName.startsWith('sys')){
if(fieldValue.nil()) {
current.setValue('u_new', true);
break;
} else {
current.setValue('u_new', false);
}
}
}
}
})(current, previous);
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-15-2021 09:33 AM
wouldnt it go to the else statement if the fields are not null? Once I fill out all the fields it goes to the else statement but it does not set the value to false it keeps it at true.