How To Get The Current CRUD Operation Name In A Script, with current.operation()

In my experience as a ServiceNow developer, understanding the context of data manipulation is crucial, particularly when working with business rules. This is where the operation() method in GlideRecord queries becomes invaluable. It allows you …

Get the name of the current operation

In my experience as a ServiceNow developer, understanding the context of data manipulation is crucial, particularly when working with business rules.

This is where the operation() method in GlideRecord queries becomes invaluable. It allows you to determine whether the current operation is an insert, update, or delete. Also known as CRUD.

Identifying the nature of the operation enables the creation of more dynamic, versatile business rules that can respond differently based on the action being performed on a record.

This method solves the problem of writing separate business rules for different operations, thereby streamlining the process.

Strategic Implementation in Business Rules

operation() is primarily used in server-side scripting, particularly within business rules. In terms of performance, using operation() is efficient as it simplifies the logic needed to handle different types of record manipulations. Best practices include using this method to tailor the behavior of business rules according to the operation type, ensuring that the script is both efficient and secure. It’s important to handle the outcome of operation() carefully to maintain data integrity and avoid unintended consequences.

A Real-World Example with a Different Table

In this business rule, I utilize the operation() method to determine the type of operation being performed on an asset record. Depending on whether it’s an insertion, update, or deletion, a specific message is logged, providing clear and concise audit trails for asset transactions.

Why This Script Matters

This example showcases the power of operation() in adapting the behavior of a script based on the context of the data operation. It’s a simple yet effective way to handle multiple scenarios within a single business rule, keeping the code clean and manageable.

Ensuring Script Effectiveness

When using operation(), it’s crucial to clearly understand the various operations that can trigger your business rule and ensure that your script logic accurately reflects these scenarios. It needs to be run on the server side. This approach not only streamlines the development process but also enhances the maintainability of your business rules in the long run.

(function executeRule(current, previous /*null when async*/) {

    var operationType = current.operation();

    switch (operationType) {
        case "insert":
            gs.info("New asset created with ID: " + current.sys_id);
            break;
        case "update":
            gs.info("Asset updated. ID: " + current.sys_id);
            break;
        case "delete":
            gs.info("Asset deleted. Previous ID: " + previous.sys_id);
            break;
        default:
            gs.info("Unknown operation performed on asset.");
    }

})(current, previous);

Let us know in the comments if you have any questions about the script.

I think I’ve only ever seen it done with current.operation();

Common Mistakes and Performance Insights

A common error with operation() is misinterpreting the operation type, leading to incorrect script behavior.

Always ensure the logic corresponds accurately to the operation type. Additionally, avoid overcomplicating scripts with unnecessary checks. operation() should be used to simplify and streamline business rules, not to add unnecessary complexity.


In summary, operation() is a powerful tool for ServiceNow developers, enabling more intelligent and context-aware business rules.

By using it wisely, developers can create scripts that are adaptable, efficient, and maintain the integrity of their data operations.



What Do You Think About This Article?

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x