Business Rule: Before BR Type Use Case.

Akshay Pithore
Tera Contributor

1.0 Business Rule:

  • Server-side script
  • Runs when record is displayed, inserted, updated, or deleted or table is queried.
  • Table for business rules – sys_script.
  • Types of business rule: Before, after, Async and Display.
  • Two steps of business rule: when to run: the time when BR is configured for the record being modified or accessed and action.
  • The Type of database operation: insert, update, query and delete.
  • Global variables in business rules:
  • Current: current now this is something which basically store state of the same record you are working on.
  • Previous: Basically, captures the previous state of the record you are working on before any update or delete.
  • G_scratchpad:  object is available on display type of business. If you want field value from database server end on a client that time, use g_scratchpad.
  • Glidesystem:
  • Actions Performed by business rule:
  • Update the field value after creating the record as per the values of other fields.
  • Stop users updating or inserting records if invalid data is used.
  • Display information message when record is inserted.
  • Create child tasks when parent is created.

1.1 Before: Before BR is triggered when user submits the form however before any action is taken on the record in the database.

  • Major use case of Before BR.
  • Abort update of record if form does not have expected value.
  • Populate assignment group before creation of incident record.
  • Users should not delete a record creation of incident record.
  • Users should not delete a record if the condition does not match.
  • The date should be updated before inserting a record.
  • Without script scenario:
  1. Scenario(insert): User should not be able to create new incident if category is inquiry/help and assignment group is not service desk.

Steps:

  1. Navigate to All>Business Rule > Create New
  2. Fill all required fields like Name, table (Incident) and active and advanced true.
  3. When to Run
  4. Actions

 

 

AkshayPithore_0-1751787634709.png

 

 

 

AkshayPithore_1-1751787634717.png

 

AkshayPithore_2-1751787634719.png

 

  • Scenario (update): Change the state of incident to in progress when user updates work notes.

Steps:

  1. Navigate to All>Business Rule > Create New
  2. Fill all required fields like Name, table (Incident) and active and advanced true.
  3. When to Run
  4. Actions

 

AkshayPithore_3-1751787684268.png

 

AkshayPithore_4-1751787684269.png

 

AkshayPithore_5-1751787684271.png

 

 

Note: when form save and adding work notes immediately state changes to in progress.

 

  • Scenario (delete): If State is new then user should not be able to delete the incident.

Steps:

  1. Navigate to All>Business Rule > Create New
  2. Fill all required fields like Name, table (Incident) and active and advanced true.
  3. When to Run
  4. Actions

AkshayPithore_6-1751787724075.png

 

AkshayPithore_7-1751787724076.png

 

AkshayPithore_8-1751787724078.png

 

 

  • With Script Scenario:
  • Scenario (Insert): User should not be able to create new incident if there is an existing incident with same short description and with same caller.

Steps:

  1. Navigate to All>Business Rule > Create New
  2. Fill all required fields like Name, table (Incident) and active and advanced true.
  3. When to Run
  4. Actions
  5. Advanced:

// This function checks if there is an existing incident record with the same short description and caller ID

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

              // Create a new GlideRecord object for the 'incident' table

              var gr = new GlideRecord('incident');

              // Add a query condition to filter incidents by the current short description

              gr.addQuery('short_description', current.short_description);

              // Add another query condition to filter incidents by the current caller ID

              gr.addQuery('caller_id', current.caller_id);

              // Execute the query to retrieve matching incident records

              gr.query();

              // If there is at least one matching incident record

              if (gr.next()) {

                             // Set the abort action flag to prevent creating a new record

                             current.setAbortAction(true);

                             // Add an error message indicating that a new record cannot be created because an existing one already exists

                             gs.addErrorMessage("You cannot create a new record as you already have an existing one.");

              }

}

 

// Call the executeRule function with the current and previous parameters

executeRule(current, previous);

 

AkshayPithore_9-1751787788164.png

 

AkshayPithore_10-1751787788166.png

 

AkshayPithore_11-1751787788168.png

 

 

  • Scenario (update): User should not be able to change the state of story to work in progress if there is no scrum task created in the story.

Steps:

  1. Navigate to All>Business Rule > Create New
  2. Fill all required fields like Name, table () and active and advanced true.
  3. When to Run
  4. Actions
  5. Advanced:

 

AkshayPithore_12-1751787824253.png

 

AkshayPithore_13-1751787824255.png

 

  • Scenario (Delete): Configuration item cannot be deleted if there are tasks or incidents associated with same CI.

Steps:

  1. Navigate to All>Business Rule > Create New
  2. Fill all required fields like Name, table (Incident) and active and advanced true.
  3. When to Run
  4. Actions
  5. Advanced:

 

AkshayPithore_14-1751787824257.png

 

AkshayPithore_15-1751787824258.png

 

AkshayPithore_16-1751787824259.png

 

Note: Go to My work> task > List view> delete any CI That have task attached and verify.

 

  • Scenario (Query): Only operation CI Should be queried if user is not admin.

Steps:

  1. Navigate to All>Business Rule > Create New
  2. Fill all required fields like Name, table (Incident) and active and advanced true.
  3. When to Run
  4. Actions
  5. Advanced:

 

 

AkshayPithore_17-1751787862034.png

 

AkshayPithore_18-1751787862035.png

 

Note: When user has admin role

AkshayPithore_19-1751787862036.png

 

Note: When user not admin role

AkshayPithore_20-1751787862037.png

 

 

 

 

 

 

 

 

 

 

 


Please mark this response as correct or helpful if it assisted you with your question.
0 REPLIES 0