Set value only if the field is empty

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-22-2022 03:02 AM
Hi guys,
I wanna make changes to current.project field only when the field is empty. Please check my last nested if statment and tell me what am I doing wrong. Currently my code updates current.project field even if there is a value in the field.
(function executeRule(current, previous /*null when async*/) {
var grStory = new GlideRecord("sn_safe_story");
grStory.addQuery("number", current.sn_safe_story.number);
grStory.query();
if (grStory.next()) {
var feat = grStory.sn_safe_feature.sys_id;
}
var grFeat = new GlideRecord('sn_safe_feature');
grFeat.addQuery('sys_id', feat);
grFeat.query();
if (grFeat.next()) {
if (current.project.length == 0){
current.project = grFeat.u_rel_project;
}
}
})(current, previous);
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-22-2022 03:16 AM
Hi, try changing "current.project.length == 0" to "current.project.nil()".
Here I'm assuming this is a reference field. If this is a List field, you would have to consider a whole lot more scenarios...

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-22-2022 04:17 AM
Hi Tomasz,
Thank you for assistance. I have tried your suggestion, it still overwrites anyways. And, yes, it is a referenced field.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-22-2022 04:45 AM
Hi,
try this
I assume your BR is Before and not After
(function executeRule(current, previous /*null when async*/) {
var grStory = new GlideRecord("sn_safe_story");
grStory.addQuery("number", current.sn_safe_story.number);
grStory.query();
if (grStory.next()) {
var feat = grStory.sn_safe_feature.sys_id;
}
var grFeat = new GlideRecord('sn_safe_feature');
grFeat.addQuery('sys_id', feat);
grFeat.query();
if (grFeat.next()) {
if (!current.project.nil() && current.project != undefined){
current.project = grFeat.u_rel_project;
}
}
})(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
‎09-22-2022 04:59 AM
Hi Ankur,
Thank you for assisting me. I have tried your solution with (current.project.nil() || current.project == undefined) condition, because I want to update when the field is empty. The solution didn't help. And also my BR is on before.