ValidateScriptForCurrentDotUpdate

  • Release version: Xanadu
  • Updated August 1, 2024
  • 2 minutes to read
  • Summarize
    Summarized using AI
    This content was generated using new OpenAI-powered functionality. Results are provided on an as is basis and are not guaranteed to be accurate or complete.

    Summary of ValidateScriptForCurrentDotUpdate

    TheValidateScriptForCurrentDotUpdatevalidator identifies workflow activities containing scripts that use thecurrent.update()function. Usingcurrent.update()within workflow scripts can lead to significant performance issues and may cause the instance to hang or crash due to infinite recursive loops. This validator issues warnings to help prevent these problems.

    Show full answer Show less

    Key Features

    • Validation Results:
      • Valid: No current.update() calls found in workflow scripts.
      • Invalid: Workflow scripts contain one or more current.update() calls, with a count of occurrences reported.
    • Severity Level: Warning, indicating a risk of degraded performance or server crashes.
    • Suggested Action: Remove all current.update() calls from workflow activity scripts.
    • Publishable and Runnable: This validator can be published and executed to check workflows.

    Why Avoid current.update() in Workflows

    Workflows operate within a transaction where the current record is updated or inserted at the end. Explicit calls to current.update() during the transaction cause unnecessary database transactions and may trigger unstable or infinite loops, especially when the record has not yet been inserted.

    Workflows may be triggered by script engines on insert or update events, and changes to current are automatically tracked and committed once the transaction completes. Therefore, forcing an explicit update is redundant and harmful.

    Practical Guidance for ServiceNow Customers

    • Do not use current.update() in workflow scripts. Instead, set or reference fields on the current object as needed; the platform automatically handles persistence.
    • Understand transaction management: Edits to current are managed in-memory and committed at the transaction’s end without explicit update calls.
    • Use the validator regularly: Run the ValidateScriptForCurrentDotUpdate validator to detect and remediate any scripts with problematic current.update() calls.

    Troubleshooting

    If you encounter performance degradation or workflow hangs, verify that no workflow scripts call current.update(). Remove such calls to restore stability and improve transaction processing efficiency.

    The ValidateScriptForCurrentDotUpdate validator finds workflow activities with scripts that use the current.update() function.

    Calling current.update() causes significant performance delays in transaction processing and might cause an instance to hang.

    Validation summary

    • Risk: At best, a workflow that uses current.update() in scripts experiences degraded performance. In the worst case, the workflow enters an infinite, recursive loop that crashes the server.
    • Severity Level: Warning
    • Valid Result: Valid
    • Valid Message: The JavaScript in this workflow has no instances of 'current.update()'.
    • Invalid Result: Invalid
    • Invalid Message: This workflow uses 'current.update()' in <count of current.update references> JavaScript statements.
    • Suggested Action: Remove current.update() from the activity scripts cited by this validator. Workflows execute within a transaction, and current is updated, or possibly inserted, at the end of the transaction, as appropriate. There is no need to explicitly update the record during the transaction.
    • Publishable: Yes
    • Runnable: Yes

    Problems with current.update() in workflow scripts

    A workflow initiates execution in one of these ways:
    • Script Engine: If a workflow is assigned to a specific table, and given a run condition, the workflow runs on INSERT.
    • Script: Any business rule, script include, background script, or client script can initiate a workflow using the workflow script include and calling startFlow().

    The workflow engine initiates a workflow based on the matched criteria of the current record being inserted. The transaction for current is managed by the script engine and not the workflow. Workflows that progress on the update() of the current record are not invoked via the workflow engine, but as a call from either a client script or business rule. In either case, the script engine is invoked, and the current record is put in memory. Edits and modifications to any current fields are made and are available to other activities and scripts that are executed in the same transaction.

    When appropriate, other engines that run in sequence with the workflow engine, such as the business rule engine or field normalization, are invoked against the same current record transaction. Any changes made to current through these scripts and activities modify the record in memory. These changing values are available for reference in any other transactions called from activities and scripts in the same INSERT transaction. When all expected changes are executed, the current record is inserted.

    When one of these scripts calls current.update() on a record that has yet to be inserted, the action forces an unnecessary and error-prone database transaction. If a record is not yet in the database, it cannot be updated. Business rules that trigger on update() on a record that is in the process of being inserted can cause a very unstable and potentially infinite looping condition.

    Troubleshooting

    This validator detects the use of current.update() in any of the editable script fields. Do not call current.update() from within a workflow script. In the event of an INSERT or UPDATE of current, the changes made to current are available to all scripts executing in the same transaction, and the script engine stores all changes in the database. Leave the update of current to the engine. Use the scripts only for setting and referencing the current field values.