Set value only if the field is empty

Options
- 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:
- Labels:
-
Scripting and Coding
7 REPLIES 7
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-22-2022 05:55 AM
Hi,
update as this
(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
Regards,
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-22-2022 05:03 AM
your variable feat is declared in local.
Please try below which is combined with Ankurs answer.
(function executeRule(current, previous /*null when async*/) {
var feat = "";
var grStory = new GlideRecord("sn_safe_story");
grStory.addQuery("number", current.sn_safe_story.number);
grStory.query();
if (grStory.next()) {
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);

Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-22-2022 05:41 AM
Hi,
Please try this:
(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 = grFeat.u_rel_project;
}
}
}
})(current, previous);