ValidateScriptForCurrentDotUpdate

  • Release version: Yokohama
  • Updated January 30, 2025
  • 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 call thecurrent.update()function. Usingcurrent.update()within workflow scripts can cause significant performance issues, including transaction delays and potentially causing the instance to hang or crash due to infinite recursive loops.

    Show full answer Show less

    Key Features

    • Risk Detection: Flags performance degradation and server crash risks associated with current.update() usage.
    • Validation Outcomes:
      • Valid: No instances of current.update() found in workflow scripts.
      • Invalid: Counts and reports how many times current.update() appears in scripts.
    • Suggested Action: Remove all instances of current.update() from workflow activity scripts.
    • Publishable and Runnable: The validator can be published and executed as part of workflow validation processes.

    Why Avoid current.update() in Workflow Scripts?

    Workflows operate within a transaction where the current record is automatically updated or inserted at the end of the transaction. Explicitly calling current.update() is unnecessary and harmful because:

    • It triggers redundant and error-prone database transactions.
    • Calling current.update() on a record not yet inserted can cause errors.
    • Business rules triggered on updates during an insert operation can lead to unstable behavior and infinite loops.

    The ServiceNow script engine manages all changes to the current record in memory and commits them at transaction completion. Therefore, workflow scripts should only set or reference current fields without explicitly updating the record.

    Troubleshooting and Best Practices

    • Use this validator to detect and eliminate any use of current.update() in workflow scripts.
    • Do not call current.update() within workflows; allow the platform's transaction engine to handle database updates.
    • Rely on script logic to modify current field values only, ensuring changes are committed correctly and efficiently.

    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.