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

Tom Sienkiewicz
Mega Sage

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...

Hi Tomasz,

Thank you for assistance. I have tried your suggestion, it still overwrites anyways. And, yes, it is a referenced field.

Ankur Bawiskar
Tera Patron
Tera Patron

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

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

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.