Javascript: Trying to write a variable (not value) to string if the variable value is true.

Joshua Camacho
Tera Guru

Hello ServiceNow community.

I am trying to write a script that takes an array of variables that hold a boolean value, checks the value, and if the value is true, writes the variable as a string in a certain format ("lab","test","poc" etc). This is for the script step in an flow designer action.

 

I apologize in advance, I am still learning javascript, so if there is a better way to do this, I am open to ideas. I am having a hard time with the if statement that will check the variable value for true and then write the variable name in a string.

 

Here is what I have so far:

 

(function execute(inputs, outputs) {
  var lab = inputs.lab; //lets say this is a true value
  var prod = inputs.prod;
  var poc = inputs.poc; //lets say this is a true value
  var dev = inputs.dev;
  var test = inputs.test;
  var uat = inputs.uat;
  var stage = inputs.stage; //lets say this is a true value
  var train = inputs.train;

//ignore the below variables
  var sqlDatabase = inputs.sqlDatabase;
  var dataBricks = inputs.dataBricks;
  var appService = inputs.appService;
  var azureKuber = inputs.azureKuber;

 // building an array of different enviroments
  var enviromentArray = [lab,prod,poc,dev,test,uat,stage,train];
  var trueEnviro;
   for (i = 0; i < enviromentArray.length; i++){
//If the variable is true then the variable name will be written to a string
//Expected final string should look like this: "lab","poc","stage"
     if (enviromentArray[i] == true){
        if (trueEnviro == "") {
          trueEnviro += '"' + x + '"';
        } else {
          trueEnviro += ',"' + x + '"';
     }
//The output variable will have the final string containing the variable names that were true
      outputs.EnviromentOutput = trueEnviro;
  }
  }
  
})(inputs, outputs);

  Hopefully, you can help me solve this dilemma. Thank you

1 ACCEPTED SOLUTION

Amit Gujarathi
Giga Sage
Giga Sage

Hi @Joshua Camacho ,
I trust you are doing great.
Here's some code that should accomplish that:

(function execute(inputs, outputs) {
  var lab = inputs.lab; // Let's say this is a true value
  var prod = inputs.prod;
  var poc = inputs.poc; // Let's say this is a true value
  var dev = inputs.dev;
  var test = inputs.test;
  var uat = inputs.uat;
  var stage = inputs.stage; // Let's say this is a true value
  var train = inputs.train;
  
  // Initialize an empty array to hold the names of the true variables
  var trueEnvironments = [];
  
  // Check each variable to see if it's true
  if (lab) {
    trueEnvironments.push("lab");
  }
  if (prod) {
    trueEnvironments.push("prod");
  }
  if (poc) {
    trueEnvironments.push("poc");
  }
  if (dev) {
    trueEnvironments.push("dev");
  }
  if (test) {
    trueEnvironments.push("test");
  }
  if (uat) {
    trueEnvironments.push("uat");
  }
  if (stage) {
    trueEnvironments.push("stage");
  }
  if (train) {
    trueEnvironments.push("train");
  }
  
  // Use the Array.join method to combine the true environment names into a single string, formatted as specified
  outputs.EnvironmentOutput = trueEnvironments.map(function(name) {
    return '"' + name + '"';
  }).join(",");
})(inputs, outputs);

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



View solution in original post

4 REPLIES 4

Rajyasri
ServiceNow Employee
ServiceNow Employee

//Inputs array might be something like this

var inputs = {"lab":true, "poc":true,"stage": true, "train": false};

 

//define what all envs(keys) you want to check in the inputs JSON

var envs = ["lab","prod","poc","dev","test","uat","stage", "train"];
var result = [];

for (var i=0; i<envs.length; i++){
if(!gs.nil(inputs[envs[i]]) && inputs[envs[i]])
result.push(envs[i]);
}
gs.info(result); //lab,poc,stage

 

Sandeep Rajput
Tera Patron
Tera Patron

@Joshua Camacho Here is the updated script from my side.

 

    // Add your code here
    (function execute(inputs, outputs) {
        var environmentObj = {
            'lab': inputs.lab,
            'prod': inputs.prod,
            'poc': inputs.poc,
            'dev': inputs.dev,
            'test': inputs.test,
            'uat': inputs.uat,
            'stage': inputs.stage,
            'train': inputs.train
        };
        //ignore the below variables
        var sqlDatabase = inputs.sqlDatabase;
        var dataBricks = inputs.dataBricks;
        var appService = inputs.appService;
        var azureKuber = inputs.azureKuber;

        // building an array of different enviroments        
        var trueEnviro = [];
        for (var envir in environmentObj) {
            //If the variable is true then the variable name will be written to a string
            //Expected final string should look like this: "lab","poc","stage"
            if (environmentObj[envir] == 'true' || environmentObj[envir] == true) {
                trueEnviro.push(envir);
                //The output variable will have the final string containing the variable names that were true
            }
        }
        outputs.EnviromentOutput = trueEnviro.toString(); //This will give you a comma separated string of enviroments

    })(inputs, outputs);

 

Hope this helps.

Amit Gujarathi
Giga Sage
Giga Sage

Hi @Joshua Camacho ,
I trust you are doing great.
Here's some code that should accomplish that:

(function execute(inputs, outputs) {
  var lab = inputs.lab; // Let's say this is a true value
  var prod = inputs.prod;
  var poc = inputs.poc; // Let's say this is a true value
  var dev = inputs.dev;
  var test = inputs.test;
  var uat = inputs.uat;
  var stage = inputs.stage; // Let's say this is a true value
  var train = inputs.train;
  
  // Initialize an empty array to hold the names of the true variables
  var trueEnvironments = [];
  
  // Check each variable to see if it's true
  if (lab) {
    trueEnvironments.push("lab");
  }
  if (prod) {
    trueEnvironments.push("prod");
  }
  if (poc) {
    trueEnvironments.push("poc");
  }
  if (dev) {
    trueEnvironments.push("dev");
  }
  if (test) {
    trueEnvironments.push("test");
  }
  if (uat) {
    trueEnvironments.push("uat");
  }
  if (stage) {
    trueEnvironments.push("stage");
  }
  if (train) {
    trueEnvironments.push("train");
  }
  
  // Use the Array.join method to combine the true environment names into a single string, formatted as specified
  outputs.EnvironmentOutput = trueEnvironments.map(function(name) {
    return '"' + name + '"';
  }).join(",");
})(inputs, outputs);

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



Thank you, that is exactly what I was looking for!! I learned a lot from your code.