Check if global variable exists fails

Hi,

I’m trying to initialize a global variable in case the variable doesn’t exist yet, but for some reason Kantu fails to evaluate the Javascript and returns an error.

I tried all of the following, all with no luck:

executeScript_Sandbox | return ${globalVar} || new Array(); | globalVar
if_v2 | !${globalVar} 

They all throw an error saying variable “GLOBALVAR” is not defined

Shouldn’t this work? And any ideas how to overcome this?

Same here. Looks like it should not but I wish it would.

You can check if a variable is defined this way:

  • Set !errorignore to true
  • use the variable, e. g. with store
  • Check the result with !LastCommandOK

Check if defined: (Since this is a global variable, it is defined at the second run of the macro, because we define it after the check!)

{
      "Name": "checkvar",
      "CreationDate": "2019-6-6",
      "Commands": [
        {
          "Command": "store",
          "Target": "true",
          "Value": "!errorignore"
        },
        {
          "Command": "store",
          "Target": "${globalvar5} ",
          "Value": "justatest"
        },
        {
          "Command": "if_v2",
          "Target": "${!lastcommandOK} == false",
          "Value": ""
        },
        {
          "Command": "store",
          "Target": "aaaaaaa",
          "Value": "globalvar5"  <=== now we define it!!!
        },
        {
          "Command": "else",
          "Target": "",
          "Value": ""
        },
        {
          "Command": "echo",
          "Target": "it was already defined",
          "Value": "green"
        },
        {
          "Command": "end",
          "Target": "",
          "Value": ""
        }
      ]
    }

The question is why is it even throwing an error? If it’s not defined why not return ND or None?

That doesn’t really help in my case.

Here’s my use case:

I’m running a Macro that opens a list of websites. Each website has a dedicated sub-macro that gets invoked by the main macro to collect data from that site. Sometimes one of these site fails for whatever reason… In which case, I invoke the main Macro once again to process everything once again.

But unfortunately, currently the Macro is processing every website, including the ones that were processed successfully. Instead, I’d like to be able to set a flag - a global variable (something like isCompleted) for each site that failed, and only process those.

Technically, global variables should be able to work because they retain values after the macro stops. But it’s not working at the moment because it throws an error when I check for the existence of the global variable.

Hope that makes sense.

@rafael777 But the macro snippet I posted allows exactly this, or? It checks for the existence of a global variable, and if it is not defined, it defines it. It might not be the most elegant solution, but it should work for your use case. But if not, please tell me why not.

You’re right. I spoke too soon. It’s a bit cumbersome, but it does work :slight_smile:

Thanks!