---
category: [Administration & Integrations, Platform and Product Extensions, Platform and Product Extensions/Integration, Administration & Integrations/Custom Integrations & Apps, Platform and Product Extensions/Workday Extend]
keyword: [Extend, PMD Scripting, Scripts]
nav: wcp_docs
parent_url: /wcp_docs/
title: >- 
  Reference: PMD Scripting Syntax
layout: subsection
---

## <a id="section_languageElements"></a>Language Elements

-   <b>Comments</b>

    Start a single comment line with `//`. Example:

    ```
    // This is a comment.
    ```

    Enclose multiple comment lines with `/* */`. Example:

    ```
    /* This is a
    multi-line comment. */
    ```

-   <b>Identifiers and variables</b>

    Start identifiers and variables with `a-z`, `A-Z`, `_`, or `$`. You can then use `0-9`, `a-z`, `A-Z`, `_`, or `$`. Examples:

    Valid: `var1, _a99, $1`

    Invalid: `9v, !a99, 1$`

    Variable names are case-sensitive. Example: `var1` and `Var1` are different variables.

    <b>Reserved keywords</b>

    Don't use these keywords in variable names:

    -   `and`
    -   `break`
    -   `catch`
    -   `const`
    -   `continue`
    -   `do`
    -   `div`
    -   `else`
    -   `eq`
    -   `false`
    -   `finally`
    -   `for`
    -   `function`
    -   `ge`
    -   `gt`
    -   `if`
    -   `instanceof`
    -   `lt`
    -   `le`
    -   `let`
    -   `mod`
    -   `ne`
    -   `new`
    -   `not`
    -   `null`
    -   `or`
    -   `return`
    -   `throw`
    -   `true`
    -   `try`
    -   `typeof`
    -   `var`
    -   `while`

    <b>Using the dot notation</b>

    You can use the dot (`.`) notation to access a variable as an object property. Note these guidelines when using the dot notation:

    -   Don't use reserved keywords in variable names or properties when using the dot notation.
    -   An equivalent of the dot notation is the bracket notation. You can use the bracket notation if the property contains a reserved keyword. Example: Instead of <i>event.for.id</i>, use <i>event['for'].id</i>.
    -   Don't use any of the operator symbols in a dot notation. For the list of operators, see [Operators](/wcp_docs/snh1581402351791.html#section-operators).

        <b>Note:</b> PMD Scripting interprets operators within the dot notation. A validation error occurs if a variable name using the dot notation contains an operator. Instead of using the dot notation, use the equivalent bracket notation. Example: PMD Scripting interprets the hyphen character `-` in <i>someVariable.a-b</i> as a subtraction operator, and causes an error. Instead of <i>someVariable.a-b</i>, use <i>someVariable['a-b']</i>.


-   <b>Scripts</b>

    A PMD script:

    -   Consists of zero or more statements.
    -   By default, returns the last expression the script evaluated. A script also returns the expression that follows the `return` keyword, or null if nothing follows it.

-   <b>Local variables</b>

    The `const`, `let`, and `var` keywords define the scope accessibility of local variables in your script. These keywords also help with readability, indicating the intention of these variables to the consumer of the code.

    To promote defensive programming, the best practice is to use `const` whenever possible. If your business logic prevents the use of `const`, use `let` to limit the variable scope. Otherwise, then finally use `var`.

    -   <b>const</b>

        Defines block level variables whose values can't be changed. A block-level variable is a variable that’s declared within any script logic enclosed in `{ }`, and is accessible only within that block.

        Use `const` to guard against accidentally modifying a value that shouldn't be updated.

        Examples:

        This sample assigns an initial value to a `const` variable. Note that an error occurs if the script attempts to change the value of the `const` variable.

        ```
        const tenant = 'gms';
        tenant = 'amu';  // Causes an error because a new value cannot be assigned to a const variable.
        ```

        This next sample assigns an initial value to the tenant variable within an `if` block. Note that an error occurs if the script attempts to access the tenant variable outside the `if` block.

        ```
        if (condition) {
         const tenant = 'gms';
        }
        console.info(tenant); // Causes an error because 'tenant' is out of its accessible scope.
        ```

    -   <b>let</b>

        Defines block level variables that can have reassigned values within the block. A block-level variable is a variable that’s declared within any script logic enclosed in `{ }`, and is accessible only within that block.

        Use `let` to:

        -   Guard against accidentally creating variables that have broader scope than they should. The `let` keyword keeps the scope of the variable small and referenceable only within the block where it's defined.
        -   Ensure that the variables become out of scope upon exiting the block.

        Examples:

        This sample assigns an initial value to the <i>tenant</i> variable, and reassigns a new value:

        ```
        let tenant = 'gms';
        tenant = 'amu';  
        ```

        This next sample assigns an initial value to the tenant variable within an `if` block. Note that an error occurs if the script attempts to access the tenant variable outside the `if` block.

        ```
        if (condition) {
         let tenant = 'gms';
        }
        console.info(tenant); // Causes an error because 'tenant' is out of its accessible scope.
        ```

    -   <b>var</b>

        Defines local variables using the `var` keyword. Their identifying rules are the same as contextual variables:

        -   Basic declaration: `var x;`
        -   Declaration with assignment: `var theAnswer = 42;`
        -   Invalid declaration: `var x.y;`

        Their scope is the entire script scope and they take precedence in resolution over contextual variables. When you create a script with named parameters, they behave as local variables.

        The `var` keyword defines a larger scope accessibility than `let`. You can access the `var` variables within an entire function while `let` only allows access within a block.


-   <b>Statements</b>

    A statement can be:

    -   The empty statement with just a semicolon (`;`).
    -   A block.
    -   An assignment.
    -   An expression.

    Optionally terminate statements with the semicolon (`;`).

-   <b>Block</b>

    A block is a set of multiple statements inside curly braces. Example: `{, }`

-   <b>Assignment</b>

    An assignment sets the value of a variable. Example: `myVar = 'a value'`

-   <b>Method calls</b>

    Calls a method of an object. This example calls the `upperCase` method of the "hello world" string.

    `"hello world".upperCase()`

    For overloaded methods and multiple arguments, PMD Scripting makes the best effort to find the most appropriate, nonambiguous method to call. Note that the sample `upperCase` method is a PMD Scripting instance method, not a member of `java.lang.String`.

-   <b>String literals</b>

    Enclose a string literal with 2 backtick characters `\` \``, which support string interpolation with embedded expressions and multiline strings.

    Examples:

    ```
    // #1 - template string
    var name = 'Logan McNeil';
    var greeting = `Hi, {{name}}`;
    // "Hi, Logan McNeil"
    
    // #2 - multiline string
    var name = 'Logan McNeil';
    var message = `;
     Hi, {{name}}
     How are you doing?
    `;
    // "Hi, Logan McNeil
    //  How are you doing?"
    ```



## <a id="section_functions"></a>Functions

-   <b>empty</b>

    Evaluates whether an expression is 'empty'. The `empty` function returns true when the argument is:

    -   `null`
    -   An empty string
    -   An array of length zero
    -   A collection of size zero
    -   An empty map
    -   Defining a method '`public boolean isEmpty()`' that returns `true` when the instance is empty.

    The `empty` function returns false in other cases (besides errors). Example:

    ```
    empty(arg)
    ```

-   <b>size</b>

    Evaluates the 'size' of an expression. The `size` function returns:

    -   0 if the argument is null.
    -   Length of an array.
    -   Length of a string.
    -   Size of a Collection.
    -   Size of a Map.
    -   The result of calling a method '`public int size()`' defined by the argument class.

    The `size` function returns 0 in other cases (besides errors). This example returns 5:

    ```
    size("Hello")
    ```

-   <b>function</b>

    Defines a function within the script, usually associated with a local variable assignment. Example:

    ```
    var fun = function(x, y) { x + y }
    ```

    Calling a function follows the standard convention: `fun(17, 25)`

-   <b>closure</b>

    Defines an anonymous function. Example: `(x) => { x + 1}`

    Some PMD Scripting functions accept a closure function as a parameter. This sample snippet calls the PMD Scripting function `find` with a closure function, <i>item => {item.id == id}</i>:

    ```
    var target = someEndpoint.data.find(item => {item.id == id});
    ```

    Within a closure, you can't redefine the variables that are declared outside the closure.

    Example:

    ```
    const items = [1, 2, 3];
    
    // This doesn't work because the closure is redefining the value of closureInt.
    let closureInt = 0;
    items.forEach(item => { closureInt++; });
    
    // This works because the closure is adding items to the existing closureList, not redefining the list.
    const closureList = [];
    items.forEach(item => {closureList.add(item)});
    
    // This works because the for loop is not a closure.
    let loopInt = 0;
    for (let i = 0; i < items.size(); i++) {
    loopInt++;
    // > other statements...
    }
    ```



## <a id="section_operators"></a>Operators

<b>Note:</b>

-   <b>Addition `+`</b>

    Use the `+` operator. Example: `val1 + val2`

-   <b>Boolean `&&`</b>

    Use the `&&` operator as the logical AND. Example: `cond1 && cond2`

    <b>Note:</b> PMD Scripting supports the `&&` operator, not the `and` operator.

-   <b>Boolean `||`</b>

    Use the `||` operator as the logical OR. Example: `cond1 || cond2`

    <b>Note:</b> PMD Scripting supports the `||` operator, not the `or` operator.

-   <b>Boolean `!`</b>

    Use the `!` operator as the logical NOT. Example: `!cond1`

    <b>Note:</b> PMD Scripting supports the `!` operator, not the `not` operator.

-   <b>Decrement `--`</b>

    Reduces the value of the variable by 1.

    Examples:

    ```
    // #1
    let a = 1;
    a--; // Equivalent to a = a - 1;
    --a; // Equivalent to a = a - 1;
    // a is -1.
    
    // #2
    let a = 1;
    let b = a--;
    // Above statement equivalent to:
    let b = a;
    a = a - 1;
    // b is 1 and a is 0.
    
    // #3
    let a = 1;
    let b = --a;
    // Above statement equivalent to:
    a = a - 1;
    let b = a;
    // b is 0 and a is 0.            
    ```

-   <b>Division `/`</b>

    Use the `/` operator. Example: `val1 / val2`

-   <b>Equality `==`</b>

    Use the `==` operator. Example: `val1 == val2`

    null is only ever equal to null. If you compare null to any non-null value, the result is false. Equality uses the Java `equals` method.

-   <b>Greater Than Or Equal To `>=`</b>

    Use the `>=` operator. Example: `val1 >= val2`

-   <b>Greater Than `>`</b>

    Use the `>` operator. Example: `val1 > val2`

-   <b>Increment `++`</b>

    Increases the value of the variable by 1.

    Examples:

    ```
    // #1
    let a = 1;
    a++; // Equivalent to a = a + 1;
    ++a; // Equivalent to a = a + 1;
    // a is 2.
    
    // #2
    let a = 1;
    let b = a++;
    // Above statement equivalent to:
    let b = a;
    a = a + 1;
    // b is 1 and a is 2.
    
    // #3
    let a = 1;
    let b = ++a;
    // Above statement equivalent to:
    a = a + 1;
    let b = a;
    // b is 2 and a is 2.
    ```

-   <b>Inequality `!=`</b>

    Use the `!=` operator. Example: `val1 != val2`

-   <b>Less Than `<`</b>

    Use the `<` operator. Example: `val1 < val2`

-   <b>Less Than Or Equal To `<=`</b>

    Use the `<=` operator. Example: `val1 <= val2`

-   <b>Modulus (or remainder) `%`</b>

    Use the `%` operator. Example: `val1 % val2`

-   <b>Multiplication `*`</b>

    Use the `*` operator. Example: `val1 * val2`

-   <b>Negation `-`</b>

    Use the unary `-` operator. Example: `-12`

-   <b>Null-coalescing `??`</b>

    The null-coalescing operator `??` returns the value of its left-hand operand if it isn't null. Otherwise, it evaluates the right-hand operand and returns its result.

-   <b>Operator chaining `?.`</b>

    Safely accesses the property of an object or calls its method. If the object accessed or the method returns null, the expression short-circuits to return null instead of throwing an error.

    Example:

    ```
    const map = {
     'foo': {
         'bar': 'ABC'
     }
    };
    console.info(map.foo.bar.upperCase());
    console.info(map.foo.bar123?.upperCase()); // Doesn't cause an error even if 'bar123' doesn't exist.
    ```

-   <b>Range (inclusive)</b>

    The inclusive range operator `to` creates a range of numbers from 2 numbers. The number on the left is the starting number and the number on the right is the ending number. The number increments by 1. Example: `1 to 10`, `-10 to 10`

-   <b>Range (exclusive)</b>

    The exclusive range operator `until` is similar to the inclusive range, except that it excludes the last number. Example: `0 until 10`

-   <b>Subtraction `-`</b>

    Use the `-` operator. Example: `val1 - val2`

-   <b>Ternary `?:`</b>

    Use the ternary conditional operator `condition ? if_true : if_false`. You can also use the abbreviation `value ?: if_false`, which returns the value if its evaluation is defined, non-null, and non-false. Example: These statements are equivalent:

    ```
    val1 ? val1 : val2
    val1 ?: val2
    ```

    The condition evaluates to false when it refers to an undefined variable or null.

-   <b>Side-effect operators</b>

    Some operators exist in side-effect forms. Their default behavior is to execute the operator and assign the left-hand side with the result. Example: `a += 2` is equivalent to `a = a + 2`

    The operators are:

    -   `+=`
    -   `-=`
    -   `*=`
    -   `/=`
    -   `%=`



## <a id="section_1"></a>Lists, Maps, and Sets

For object collections, PMD Scripting supports lists, maps, and sets.

-   <b>Lists</b>

    A list is an ordered collection of items. Examples:

    -   `["item1", "item2", "item3"]`
    -   `[ ]` defines an empty list.

    To access list elements, use square brackets `[<i>index</i>]`. Example: `arr1[0]`

-   <b>Maps</b>

    A map is an unordered collection of key-value pairs. Each key in the map must be unique. Examples:

    -   `{ "key1": "value1", "key2": "value2", "key3": "value3"}`
    -   `{:}` defines an empty map.

    Unlike maps in JavaScript, PMD Scripting:

    -   Defines a map with a colon. Example: `const myMap = {:}`
    -   Can have any value within a map rather than just a string. However, this means that a map containing <i>{a: ‘value’}</i> refers to the variable <i>a</i>, which is different from <i>{‘a’: ‘value’}</i>. If <i>a</i> is not declared as a variable, you’ll get an error indicating it’s an undefined variable.

    If a map contains string keys, you can access the map elements using square brackets `[<i>key</i>]`. If the key is not a string, use the [map.get(key)](/wcp_docs/rmr1648433427412.html) function instead. Examples:

    ```
    const map = {"a" : "foo"};
    map["a"]; // returns "foo"
    const a = "a";
    map[a] // returns "foo"
    
    const b = 1;
    map[b] = "bar";
    map[b]; // returns null
    map.get(b); // returns "bar"
    ```

    The bracket notation and the dot notation are generally equivalent. Example: `map['name']` and `map.name` are equivalent. However, you can’t use the dot notation if the property name is a variable. Example:

    ```
    var a = 'foo';
    if (true) {
     a = 'bar';
    }
    var m = {"foo": 1, "bar": 2};
    map[a]; // returns 2
    map.a // returns null
    ```

-   <b>Sets</b>

    set is an unordered collection of keys. Each key in the set must be unique. Examples:

    -   `{"key1", "key2", "key3"}`
    -   `{}` defines an empty set.

    <b>Note:</b> In PMD Scripting, `{}` defines a set. This is different from JavaScript, which defines an object as `{}`.



## <a id="section_controlFlow"></a>Control Flow

-   <b>`if` statement</b>

    A standard `if-else` statement. Example:

    ```
    if (x * 2 < y) {
    x = y;
    } else {
    y = x;
    }
    ```

-   <b>`for`</b>

    Loops through the items of an Array or List.

    Examples:

    This sample uses the conventional for loop syntax:

    ```
    const list = [];
    for (let i = 1; i <= 10; i++) {
     list.add(i);
    }
    console.info(list); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    ```

    This sample uses the PMD Scripting syntax:

    ```
    for(let item : list) {
    x = x + item;
    }
    ```

-   <b>`while`</b>

    Loops until a condition is true. Example:

    ```
    while (x < 10) {
    x = x + 2;
    }
    ```

-   <b>`do..while`</b>

    Similar to `while` but executes the statements before testing a condition. Example:

    ```
    do {
    x = x + 2;
    } while (x < 10);
    ```

-   <b>`continue`</b>

    Skips to the next iteration in a loop (`while` and `for`).

-   <b>`break`</b>

    Breaks from a loop (`while` and `for`) unconditionally.



### Related Information

[Concept: PMD Scripting](/wcp_docs/oli1581402304020.html)

[PMD Scripting](/wcp_docs/jab1582081565510.html)

[Reference: PMD Scripting Examples](/wcp_docs/mdp1582058758619.html)

