- 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-14-2021 07:26 AM
Hi,
Can you try updating as this using setValue() method
current.setValue('u_empty', true);
OR
current.setValue('u_empty', false);
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-14-2021 07:51 AM
I tried setting it to current.setValue('u_empty', true); but it does not change the field if i update the record.
The business rule runs after I insert or update a record.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-14-2021 07:54 AM
Hi,
if that is after insert/update BR then you need to use current.update() but that is not recommended.
So please use before insert/update BR and check once.
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-14-2021 09:30 AM
I set the business rule to run before a record is inserted or updated, but it is still not changing the value of u_empty.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-14-2021 09:46 AM
Ok so now it changes the u_empty to true when it sees empty field, but when I fill the empty fields u_empty does not change back to false.