KBEC-00309 - Testing for the existence of a property before using it

Article ID:360032827832
3 minute readKnowledge base

Summary

This article describes how to test for the existence and value of a property in an ElectricCommander step before using it in 4.2.3 and above.

Some jobs need to refer to a property that might not exist, and you may need to take different actions if you cannot find the property. Regular Property Path syntax like $[maybe] works for referencing an existing property, but leads to a Property Reference error and causes the step to fail if the property does not exist.

Other jobs may require testing whether or not the value of a property is empty. The proper way to do this for different types of properties isn’t always straightforward, and the specific behavior for certain methods may vary between versions.

For versions below 4.2, please refer to KBEC-005. Versions 4.2.0-.2 contained some unexpected javascript behaviors; please see Javascript Changes to be aware of in Commander v4.2 for more details.

Solution

Use $[/javascript ...] to test the property. This will not cause an error if the property doesn’t exist.

You can find out if a custom property exists by using the typeof() function in JavaScript. If the property does not exist, typeof() will return the string "undefined".

To check if a custom property is empty, check for its existence using typeof() and compare the property’s value with the empty string.

To find out if an intrinsic property has a value, use a getProperty() call on the property and compare the return value with null.

You can also use ec-perl directly to check for a property’s existence.

Oracle DB

Be noted, that Oracle DB stores empty property value as NULL instead of empty string.

Examples

Custom Property Example

These examples use JavaScript inside a Perl step, but it can be used inside any type of step.

Shell: ec-perl
Command:

if ( $[/javascript (typeof(myJob.maybe) == "undefined") ? "1":"0"] ) {
     print "maybe is not defined\n";
}
elsif ("$[/javascript (myJob.maybe)]" eq '') {
     print "maybe is defined, but empty\n";
}
else {
     print "maybe has a value of $[/javascript (myJob.maybe)]\n";
}

Alternative syntax to check if a custom property has a non-empty value:

Shell: ec-perl
Command:

$[/javascript if(myJob.maybe != "" && typeof(myJob.maybe) != "undefined")
     'print "maybe has a value.\n";']

Intrinsic Property Example

Note that with intrinsic properties, a direct comparison to null or the empty string does not have the same behavior across all versions.

Shell: ec-perl
Command:

if($[/javascript (getProperty("/myJob/scheduleName") == null) ? "1":"0"]) {
     print "scheduleName does not have a value.\n";
}
else {
     print "scheduleName has a value of $[/javascript (myJob.scheduleName)]\n";
}

Intrinsic Property check to determine if job is in a workflow.

Shell: ec-perl
Command:

print "$[/javascript
if (getProperty("/myJob/callingState") ==  null) {
    'job is not in a workflow.\n'
} else {
    'In workflow named $[/javascript (myWorkflow.workflowName)]\n'
}
]\n";

Checking for a Property Sheet Example

Property sheets have an additional property, a propertySheetId.

Shell: ec-perl
Command:

if ( $[/javascript (typeof(myJob.scratch) == "undefined") ? "1":"0"] ) {
     print "scratch is not defined\n";
}
elsif ( $[/javascript (typeof(myJob.scratch.propertySheetId) != "undefined") ? "1":"0"] ) {
     print "scratch is defined and a property sheet\n";
}
elsif ("$[/javascript (myJob.scratch)]" eq "") {
     print "scratch as a property is defined but empty\n";
}
else {
     print "scratch as a property has a value of $[/javascript (myJob.scratch)]\n";
}

Using JavaScript to determine if -

The step is called as a procedure or subprocedure (use procedure of parentStep)

myParent is generally only useful when called from a subprocedure. You can use JavaScript to filter actions to run only in subprocedures with the following syntax.

$[/javascript if (getProperty("/myParent/subprocedure") != null) 'echo my subprocdure name is $[/javascript myParent.subprocedure]']
The step is a root step (use stepName of parentStep)

A root step means we have no parent in this step: You can use JavaScript to filter actions to run only in the root step:

$[/javascript if (getProperty("/myStep/stepName") != null) 'echo my step name is $[/javascript myStep.stepName]']
The job/step is called from in a workflow (use callingState):

If you run a procedure or step not in a workflow, use JavaScript to filter workflow references:

$[/javascript if (getProperty("/myJob/callingState") != null) 'echo my current workflow is $[/javascript myWorkflow.workflowName ]']

ec-perl

If you don’t want to use JavaScript, you can check for property values directly in ec-perl.

The following script tests for an intrinsic property:

use ElectricCommander;

my $ec = new ElectricCommander();
eval {($ec->getProperty("/myWorkflow/workflowName")->findvalue("//value")->value())};
if ($@) {
  print "workflowName is not defined. This job is not part of a workflow.";
} else {
  print "This job is a part of a workflow.";
}

This code tests for a custom property:

use ElectricCommander;

my $ec = new ElectricCommander();
my $value;
eval {($value = $ec->getProperty("/myJob/maybe")->findvalue("//value")->value())};
if ($@) {
  print "maybe is not defined.\n";
}
elsif ($value eq '') {
  print "maybe is empty.\n";
}
else {
  print "maybe is defined.\n";
}

Applies To

  • Product versions: 4.2.3 and higher

  • OS versions: all