Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Multiple if else to 'Switch' condition in Transform map

Suvetha S
Tera Contributor

Hi All,

 

I want to change the below multiple if condition to a switch statement. Please help with that

 

var targetRecord = new GlideRecord("cmdb_ci_ip_router");
if (targetRecord.get("serial_number", source.u_sn) && (source.u_sn != "")) {
return targetRecord.sys_id.toString();
} else if (targetRecord.get("name", source.u_name) && (source.u_name != "")) {
return targetRecord.sys_id.toString();
} else if (targetRecord.get("ip_address", source.u_mgmtip) && (source.u_mgmtip != "")) {
return targetRecord.sys_id.toString();
} else{
return -1;
}
})(source);

1 ACCEPTED SOLUTION

Prasad Dhumal
Mega Sage

Hello Suvetha,

You can write logic like this:

var targetRecord = new GlideRecord("cmdb_ci_ip_router");
switch(true){
    case targetRecord.get("serial_number", source.u_sn) && (source.u_sn != ""):
        return targetRecord.sys_id.toString();
    case targetRecord.get("name", source.u_name) && (source.u_name != ""):
        return targetRecord.sys_id.toString();
    case targetRecord.get("ip_address", source.u_mgmtip) && (source.u_mgmtip != ""):
        return targetRecord.sys_id.toString();
    default:
        return -1;
}

 

View solution in original post

2 REPLIES 2

Prasad Dhumal
Mega Sage

Hello Suvetha,

You can write logic like this:

var targetRecord = new GlideRecord("cmdb_ci_ip_router");
switch(true){
    case targetRecord.get("serial_number", source.u_sn) && (source.u_sn != ""):
        return targetRecord.sys_id.toString();
    case targetRecord.get("name", source.u_name) && (source.u_name != ""):
        return targetRecord.sys_id.toString();
    case targetRecord.get("ip_address", source.u_mgmtip) && (source.u_mgmtip != ""):
        return targetRecord.sys_id.toString();
    default:
        return -1;
}

 

@Prasad Dhumal  Thank you so much. It worked