KBEC-00005 - Testing for the existence of a property before using it pre-4.2

Article ID:360032832732
2 minute readKnowledge base

Description

This article describes how to test for the existence of a property in an ElectricCommander step before using it, in versions prior to 4.2. For versions above that, please see KBEC-309.

Some jobs may refer to a property that does not exist and you may need to take different actions if you cannot find the property. Using the regular Property Path syntax, like $[maybe], works for finding an existing property, but leads to a Property Reference error if the property does not exist, which then leads to a step failure.

Solution

To address this issue, use $[/javascript ...] to test the property. In JavaScript, if the property does not exist, it evaluates to an empty string and does not cause an error.

Also, you can find out if the property exists by using the "typeof" function in JavaScript. If the property does not exist, the "typeof" will be equal to the string "undefined". If the property is a Intrinsic Property the "typeof" will be equal to object type "null".

Custom Property Example

This example uses JavaScript inside a Perl step, but it can be used inside any type of step.
Shell: ec-perl
Command:

 if ( $[/javascript ( (typeof(myJob.maybe) == null)) ? "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";
}

Intrinsic Property Example

Intrinsic Properties are null when not defined - instead of "undefined".

Shell: ec-perl
Command:

 if ( $[/javascript ( (typeof(myJob.abortedBy) == null)) ? "1":"0"] ) {
     print "abortedBy is not defined\n";
}
elsif ("$[/javascript (myJob.abortedBy)]" eq "") {
     print "abortedBy is defined, but empty\n";
}
else {
     print "abortedBy has a value of $[/javascript (myJob.abortedBy)]\n";
}
Intrinsic Property check to determine if in a workflow.

Shell: ec-perl
Command:

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

Checking for a Property Sheet Example

Property sheets have an additional property, a propertySheetId.
Shell: ec-perl
Command:

 if ( $[/javascript ( (typeof(myJob.scratch) == null)) ? "1":"0"] ) {
     print "scratch is not defined\n";
}
elsif ( $[/javascript ( (typeof(myJob.scratch.propertySheetId) != null)) ? "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 (myParent.procedure != 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 (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 JavaSript to filter workflow references:

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

If you are in ec-perl and don’t want to use javascript:

eval {($ec->getProperty("/myWorkflow/workflowName")->findvalue("//value")->value())};
if ($@) {
 print "Not part of a workflow job";
} else {
 $electric_commander->createProperty("/myWorkflow/report-urls/ABC",{value=>"/commander/jobs/$[/myJob/jobId]/$[/myJobStep/workspaceName]/$abc.html"});
}