Forum Discussion

Leahy's avatar
Leahy
Contributor
5 years ago

Test if variables are passed to function

I am writing a function but need to ensure all the proper variables have been passed with the call to the function...

 

From the function.....

// Obtains a variable collection
Variables = ProjectSuite.Variables;
Variables1 = Project.Variables;

if (!(Variables.VariableExists(Emp_Num) || Variables.VariableExists(Start_Date) || Variables.VariableExists(End_Date)))
{

 

I have included a copy of the watch list....the varialbes do exist....

 

What am i doing wrong with the "Variable.Exists"  ?????

 

  • Hi,

     

    Adding to the thread. If you want to check the JS variables, use this code:

     

    if (typeof yourVar === 'undefined') {
     
    }
  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    Based upon your watch list... there is only one project variable and zero project suite variables.  So, it is entirely likely that the "false" values for VariableExists are correct.    Keep in mind, that the VariableExist method is ONLY for Project or Project suite variables, not variables defined in JavaScript code.

     

    Let's say I have a project variable named "myVar".  The following code can be used.

     

    function testVariable() {
        var localVar;
        if (Project.Variables.VariableExists("myVar")) {
            Log.Message('myVar exists') //this will log because the project variable exists
        }
        if (Project.Variables.VariableExists("localVar)){
            Log.Message('localVar exists') //This will NOT log because there is no project variable by that name
        }
    
    
    }
  • S_Leonardo's avatar
    S_Leonardo
    Occasional Contributor

    The method 'VariableExists' is only applicable to variables from the objects Project.Variables and ProjectSuite.Variables and based on your screenshot, those three variables are not Project or ProjectSuite Variables.

     

    If you need to check if some value has been passed to the variable (example in VBScript):

     

    Function MyFunc(Emp_Num, Start_Date, End_Date)
    
      if (Emp_Num= "") Then
        Log.Error("Variable Emp_Num is empty.")
      end if
    
       if (Start_Date= "") Then
        Log.Error("Variable Start_Date is empty.")
      end if
    
      if (End_Date= "") Then
        Log.Error("Variable End_Dateis empty.")
      end if
     .
     .
     .
    End function

     

     

     
  • Wamboo's avatar
    Wamboo
    Community Hero

    Hi,

     

    Adding to the thread. If you want to check the JS variables, use this code:

     

    if (typeof yourVar === 'undefined') {
     
    }
    • Leahy's avatar
      Leahy
      Contributor

      Thanks for that, I had figured out a way to do it...

       

      The way I am using is..

       

      if  ((!Emp_Num) || (!Start_Date) || (!End_Date))

      {....}

       

      this catches it, if one of the vars is missing....

      • AlexKaras's avatar
        AlexKaras
        Champion Level 3

        Hi,

         

        > if  ((!Emp_Num) || (!Start_Date) || (!End_Date))

        To be precise, this catches the situations when some variable is missed, or is null or is assigned a value that evaluates to false.

         

        To check for missed variable in JS Wamboo provided correct way of check.