Ask a Question

Test if variables are passed to function

SOLVED
Leahy
Contributor

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"  ?????

 

5 REPLIES 5
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
    }


}

Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available
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
Community Hero

Hi,

 

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

 

if (typeof yourVar === 'undefined') {
 
}

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....

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.

 

Regards,
  /Alex [Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
================================
cancel
Showing results for 
Search instead for 
Did you mean: