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.

Transform map script to ignore 'insert' of certain asset types (model categories)

michaelsmith02
Tera Contributor

I have a requirement to ignore the insert of records into the hardware table if source.asset_type is a certain value. This is mapped to the Model Category field in the hardware table. I have the following onBefore script, however, it is not working.

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

var assetType = source.asset_type;

if(assetType == "Accessories" || assetType == "Components/Upgrades" || assetType == "Memory Module" || assetType == "Hybrid Work Solutions" && action == "insert"){

ignore = true;

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

 

Am I using the right approach? The mapping for Model Category in this transform map takes source.asset_type and translates it to values that exist in our system with a script. That part works and I'm not sure if it affects this script functioning properly.

 

What am I doing wrong?

 

 

1 ACCEPTED SOLUTION

Robert H
Mega Sage

Hello @michaelsmith02 ,

 

You need to put parentheses around the OR conditions, else the "action" value will only be checked for the "Hybrid Work Solutions" type.

 

if ((assetType == "Accessories" || assetType == "Components/Upgrades" || assetType == "Memory Module" || assetType == "Hybrid Work Solutions") && action == "insert") {

    ignore = true;

}

 

Or, if you want it to be more readable:

 

var typesToIgnore = [
    'Accessories',
    'Components/Upgrades',
    'Memory Module',
    'Hybrid Work Solutions'
]

if (typesToIgnore.includes(assetType) && action == "insert") {
    ignore = true;
}

 

Regards,

Robert

View solution in original post

3 REPLIES 3

Robert H
Mega Sage

Hello @michaelsmith02 ,

 

You need to put parentheses around the OR conditions, else the "action" value will only be checked for the "Hybrid Work Solutions" type.

 

if ((assetType == "Accessories" || assetType == "Components/Upgrades" || assetType == "Memory Module" || assetType == "Hybrid Work Solutions") && action == "insert") {

    ignore = true;

}

 

Or, if you want it to be more readable:

 

var typesToIgnore = [
    'Accessories',
    'Components/Upgrades',
    'Memory Module',
    'Hybrid Work Solutions'
]

if (typesToIgnore.includes(assetType) && action == "insert") {
    ignore = true;
}

 

Regards,

Robert

Ah, I see my mistake, adding parentheses around the "OR" statements worked! Thank you!

J Siva
Kilo Patron
Kilo Patron

Hi @michaelsmith02 

Your script has a logical grouping issue. The && action == "insert" condition currently applies only to "Hybrid Work Solutions" due to operator precedence.

You can modify your script using one of the following approaches:

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {
    var assetType = source.asset_type;
    if (assetType == "Accessories" || assetType == "Components/Upgrades" || assetType == "Memory Module" || assetType == "Hybrid Work Solutions" && action == "insert") {
        ignore = true;
    }
})(source, map, log, target);

OR

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {
  // Check if the asset type is one of the specified types and the action is "insert"
  if (["Accessories", "Components/Upgrades", "Memory Module", "Hybrid Work Solutions"].includes(source.asset_type) && action == "insert") {
    // Ignore the record if the condition is met
    ignore = true;
  }
})(source, map, log, target);


Regards,
Siva