- 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 11:14 AM
I tried your suggestion and it now changes to true when it detects an empty field, but it does not change back to false when I fill all the empty fields in. When I debugged again there was an undefined value that came up during the loop and I am unsure which field this is how can I check what this undefined field is?
Here is my code now:
(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() - 1; i++) {
var ge = fields.get(i);
//check if field is empty
if(ge.nil()) {
current.setValue('u_empty', true);
break;
} else {
current.setValue('u_empty', false);
}
}
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-14-2021 08:59 PM
Thank you for marking my response as helpful.
Did it work fine?
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 05:40 AM
Yes, the BR sets empty to true once there is an empty field in a record, but after you fill out the fields it does not set it back to false. When I debugged it I found there was still a undefined field but I went through and filled everything so I am unsure what is undefined I think it might be the Tags fields, but I dont know how to get rid of that since it does not show up in the dictionary.
Here is my code as of now
(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);
}
if(ge.nil()) {
current.setValue('u_new', true);
break;
} else {
current.setValue('u_new', false);
}
}
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-15-2021 05:47 AM
Hi,
update as this; you can exclude whichever fields you wish to
(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();
if(fieldName!= 'sys_tags'){
if(ge.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 05:53 AM
If you wish to skip all system fields which starts with sys_ then you can do this
(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();
if(!fieldName.startsWith('sys')){
if(ge.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