Writing a Primavera Cloud Expression Language Script

Write Primavera Cloud Expression Language scripts to dynamically determine the values of configured fields and measures that support formulas. A formula-based field defaults to a read-only calculated value. It displays the value resulting from the evaluation of the Primavera Cloud Expression Language script that defines the formula. The expected return type of the formula field script must be compatible with the configured field or measure type (Number, Date, or Text).

The following example shows how to calculate the Net Present Value (NPV) of a portfolio using a formula.

To define a formula that calculates Net Present Value:

  1. In the object selector, select a workspace.
  2. In the sidebar, select Summary & Settings.
  3. On the Summary & Settings menu, select Defaults & Options, and then select Portfolio.
  4. On the Portfolio page, select the Configured Fields tab.
  5. In the table, select Add.
    1. In the Column Label column, enter Net Present Value.
    2. In the View Column Name column, enter NPV.
    3. In the Data Type column, select Number.
    4. In the Type column, select Formula.
  6. In the Formula detail window, in the Formula Editor, enter the following:

    //Assume a rate of return of 10%

    //Calculate NPV for a 2 year period.

    def npv = 0;

    def i = 0.10;

    def year = 0;

    if (object.Portfolio_currentApprovedBudget <= 0) {
    return 0;

    } else {

    npv = npv + ((-1 * object.Portfolio_currentApprovedBudget) / Math.pow((1 + i), year));

    year = year + 1;

    npv = npv + ((-1 * object.Portfolio_currentApprovedBudget) / Math.pow((1 + i), year));

    year = year + 1;

    npv = npv + ((-1 * object.Portfolio_currentApprovedBudget) / Math.pow((1 + i), year));

    }

    return npv;

    Note: Because Primavera Cloud Expression Language does not support loops or function definition, you must write calculations that require recursion or iteration in an imperative style as shown in the previous example.

    Note: When adding a null check to a formula, you must specify the null check at the beginning of the if condition statement.

  7. In the Formula detail window, select Validate Formula.
  8. In the Formula Validation dialog box, select OK.
  9. Select Save.


Last Published Tuesday, May 21, 2024