Fields in the target table are not updating

Manasa23
Tera Contributor

HI,

I have written the below OnBefore script on Transform map on Hardware table. However it is not working ..
If both the assigned to and loc is empty then below target fields mentioned should update and if the assigned to or loc is empty , the the fields in target table should update.

Need Help !


if ((source.u_assign_to == "") && (source.u_loc == "")) {

target.assigned_to = "";
target.managed_by = "";
target.owned_by = "";
target.u_loc = "";
target.install_status = "6";
target.substatus = "1";
} else
if (source.u_assign_to != "" || source.u_loc != "") {
target.install_status = "1";
target.stockroom = "";
target.u_inuse = true;
}

Thanks,
M

1 ACCEPTED SOLUTION

shloke04
Kilo Patron

Hi @manasa

Your script is incorrect. Please use the modified version below:

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {

    // Add your code here
    if (JSUtil.nil(source.u_assign_to) && JSUtil.nil(source.u_loc)) {
        target.assigned_to = "";
        target.managed_by = "";
        target.owned_by = "";
        target.u_loc = "";
        target.install_status = "6";
        target.substatus = "1";
    } else {
        target.install_status = "1";
        target.stockroom = "";
        target.u_inuse = true;
    }

})(source, map, log, target);

Let me know if you are facing an issue. Would suggest to make use of JSUtil to check if value of source field is empty or not, I am saying this from my personal practical experience as sometimes "== blank' did not work for me. So I recommend using JSUtil.nil or JSUtil.notNil to check for empty or non empty values.

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

View solution in original post

6 REPLIES 6

Abhijit4
Mega Sage

Hi,

If your main concern is system not executing if part even if both values are empty then reason could be, in programming language empty values are stored in 3 different ways, it can null or undefined or "". 

You could try below,

if((source.u_assign_to == "" || source.u_assign_to ==undefined || source.u_assign_to ==null) && (source.u_loc == "") || source.u_loc == undefined || source.u_loc ==null)

or you can use nil() function as below

if(source.u_assign_to.nil() &&  

for more info on nil() function, you can see below documentation example

https://developer.servicenow.com/dev.do#!/reference/api/orlando/server/no-namespace/c_GlideElementSc...

Let me know if you have any further queries.

Please mark this as Correct or Helpful if it helps.

Thanks and Regards,
Abhijit

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

shloke04
Kilo Patron

Hi @manasa

Your script is incorrect. Please use the modified version below:

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {

    // Add your code here
    if (JSUtil.nil(source.u_assign_to) && JSUtil.nil(source.u_loc)) {
        target.assigned_to = "";
        target.managed_by = "";
        target.owned_by = "";
        target.u_loc = "";
        target.install_status = "6";
        target.substatus = "1";
    } else {
        target.install_status = "1";
        target.stockroom = "";
        target.u_inuse = true;
    }

})(source, map, log, target);

Let me know if you are facing an issue. Would suggest to make use of JSUtil to check if value of source field is empty or not, I am saying this from my personal practical experience as sometimes "== blank' did not work for me. So I recommend using JSUtil.nil or JSUtil.notNil to check for empty or non empty values.

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke