The CreatorCon Call for Content is officially open! Get started here.

Set value only if the field is empty

Kamva
Giga Guru

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);
7 REPLIES 7

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

Harshad Wagh
Tera Guru

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);

 

Lavlesh Garg1
Giga Expert

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);