SUBHAM_SHAW_SN
Tera Guru
Tera Guru

Handling separate actions for "Create" and "Update" operations using the Flow Designer trigger "Record is Created or Updated":


This approach can be used as an alternative to current.operation() when a Business Rule has both "Insert" and "Update" options selected.



Sample Use Case
:
 Create a problem record when an incident is created and update the work notes of the same problem record when incident is updated

 

Business Rule Approach

  • Table: Incident

  • When: After

  • Insert:

  • Update:

 

if (current.operation() == 'insert') {
    // Create Problem record
    var problemGr = new GlideRecord('problem');
    problemGr.initialize();
    problemGr.short_description = 'Auto-created from Incident: ' + current.number;
    problemGr.description = current.description;
    problemGr.correlation_id = current.getValue("number"); 
    problemGr.insert();
    current.problem_id = problemGr.sys_id;
}
else if (current.operation() == 'update' && current.problem_id) {
    // Update work notes on the related Problem
    var linkedProblem = new GlideRecord('problem');
    if (linkedProblem.get(current.problem_id)) {
        linkedProblem.work_notes = 'Incident ' + current.number + ' was updated.';
        linkedProblem.update();
    }
}

Flow Approach :

 

We’ll be leveraging the sys_mod_count field—labeled as "Updates"—to determine whether the operation is an insert or an update.
If sys_mod_count = 0, it indicates the record is being inserted (created).
If sys_mod_count > 0, it signifies the record is being updated.

Subham11_1-1751525331396.png

 

Identifying the "Insert/Create" Operation & executing the activities:

Subham11_3-1751525472328.png

Subham11_4-1751525532076.png

 

Identifying  the "Update" Operation & executing the activities:

Subham11_5-1751525620834.png

Subham11_6-1751525673112.png

 

 

 

 

Thanks & Regards,

Subham Kumar Shaw
ServiceNow Architect/Consultant
ServiceNow Community Rising Star ' 2022/2023/2024




 


 

1 Comment