Establishing Business Rules

Objective

After completing this lesson, you will be able to construct and modify business rules.

Introduction to Business Rules

Unit 5: Key Topics

In this Unit 5 lesson, you will learn about several key concepts that are critical for effectively leveraging the capabilities of SAP Field Service Management (FSM) through Business Rules.

Business Rules are pieces of custom logic used to enhance the standard FSM solution for specific customer cases. This lesson will help you understand the creation, execution, and use cases of these rules, which is essential for implementing them effectively.

You will explore the defining components of Business Rules, including main settings, trigger, and action sections. Understanding these components and their related settings and functionalities is crucial for creating and managing Business Rules.

The lesson also covers the different types of supported Business Rules, explaining the capabilities of each type and providing guidance on why Type Three rules should be the preferred choice.

You will learn how to specify the actions that a Business Rule should undertake using CoreSQL queries and JavaScript snippets. The lesson will explain how to determine the potential impacts of these actions on system performance and server load.

The process of validating and executing Business Rules is thoroughly covered in this lesson. You will learn key steps in testing these rules, interpreting result and performance indicators, and rectifying potential errors.

This lesson also covers how to administer Business Rules for system administration and server resource management. It will help you understand how Business Rules can aid in handling execution issues and provide an overview of the tools available for monitoring these rules.

Finally, the lesson emphasizes the best practices to adopt when designing, building, testing, executing, and monitoring Business Rules. Throughout the lesson, the usage of CoreSQL for data specification is prominent, and you will gain a crucial understanding of the relationship between CoreSQL queries, Business Rules, and their impact on system performance.

Business Rules: Overview

Business Rules are pieces of custom logic, used to enhance the FSM solution for customer-specific cases.

Business Rules are pieces of custom logic, used to enhance the FSM solution for customer-specific cases. The standard logic in FSM is described as "monolith" and can't be changed. Business rules can therefore only run in addition to, or on top of, the FSM standard logic. Business Rules are created on Company level and run within that context.

Before a business rule is executed, it needs to be triggered by something. That trigger can either be a specific event, or a pre-defined schedule. The specific events can -for example- refer to changes on the company database, inputs from an integrated system, or events published by FSM microservices.

After the business rule has been triggered, data is collected, conditions are checked, and actions are executed.

Business Rules can help to automate business processes, like sending notifications or assigning smartforms.

Business Rules can help to automate business processes. Specific examples of use cases for business rules include:

  • Send notifications, SMS, or email
  • assign a Workflow to an Activity
  • attach Smartforms to Activities
  • generate Reports
  • create follow-up activities
  • invoke Autoscheduling

Every FSM company comes with several embedded and sample/demo business rules. Embedded rules can be enabled as-is, or serve as examples for your own enhancements.

Business Rules can be managed under the corresponding tab on the Company page.

Business Rules (BRs) can be found under the corresponding tab on the Company page. The Business Rules list can be searched, sorted, and filtered. From here, BRs can be enabled or disabled, created or deleted, and uploaded or downloaded.

You can either modify a sample business rule by editing one of the existing rules or create one from scratch using the Create button. It is recommended to use the sample business rule as a base to start customizing FSM for your business case.

Configuration

Main constituents of a business rule.

The primary parts of a business rule are the main settings, the trigger section and the action section. These parts can be described as follows:

Main settings: This section contains the code, name, and description of the business rule. It also holds flags to enable the business rule and whether skipped executions should be logged. Finally, it contains information about the health status of the business rule, and the technical contact person(s), who should be informed if the business rule is in a critical health state.

Trigger: It is in this section that you define the event that activates the business rule. It includes the data object type, the variables, conditions, and other parameters for the business rule. When the conditions are met, the application will respond by executing the action specified in the action section.

Action: It is in this section that you define the application response to the event specified in the trigger section. For instance, if you have specified in the trigger section an event occurring on object create for a service call, in the action section you can then select Action  and Send SMS  and have the notification sent to a specific recipient.

Main settings of a business rule.

The main settings for business rules are described below.

  • Code: This is optional. It can be used as an identifier of the business rule. The best practice is to keep this short.
  • Name: The name of the business rule.
  • Description: A description of the business rule. Simple markup can be coded manually, for example, for line breaks, bold font, or bullet points.
  • Enabled: A yes or no option. If yes, the business rule will be active.
  • Technical Contact: This will include e-mail addresses of the contact persons for technical questions on the definition/execution of the business rule. It is recommended that you add multiple technical contacts. The listed person, or persons, will also be the point of contact for SAP Support in case the business rule is causing excessive resource usage or other server problems.
  • Error Threshold Percent: This field allows a user to adjust the threshold, in percentage, at which the technical contacts of a business rule are informed of business rules with failed executions. If the field is left empty, a default of 50% will be applied.
  • Log Skipped Executions: A yes or no option. If yes, then skipped executions will be shown in the execution log, which can be accessed in the maintenance view. The number of skipped execution logs is limited to the latest 100 skipped log entries per business rule. Older ones are deleted automatically via the SAP.FSM.BusinessRules.MaxSkippedLogsToKeep company setting. In this setting, you can define another limit - but with an absolute maximum of 1000.
  • Type: There are currently two types of supported business rules:
    • Type Two: This type of business rule offers full Javascript support.
    • Type Three: This type of business rule is based on the new microservice architecture and supports several different variable-, event-, and action types when compared to Type Two Business Rules. Where possible, it is advised to use Type Three. For details, please refer to the latest documentation.
Trigger settings of a business rule.

Trigger settings include:

  • Events: The event that triggers the business rule. Options for objects include the following:
    • CRUD-based events for a given data object (create, update, delete, or a combination thereof). Triggers of this kind:
      • On Object Create: If selected, the business rule will be triggered on the creation of a new object.
      • On Object Create or Update: If selected, the business rule will be triggered on either the creation of a new object or the update/modification of an existing object.
      • On Object Update: If selected, the business rule will be triggered on the update/modification of an existing object.
      • On Object Update or Delete: If selected, the business rule will be triggered when an existing object is updated or deleted.
      • On Object Delete: If selected, the business rule will be triggered when an existing object is deleted.
      • On Object Upload from ERP Connector: If selected, the business rule will be triggered when object data (for example, business partner) is uploaded using the Proaxia ECC Connector.
    • Scheduled: If selected, the business rule will be set to run at the specified times and intervals.
    • FSM Events: Field Service Management events are business events, which are published by services of SAP Field Service Management in case a related occurrence happened.
  • Object Type: The data transfer object (DTOs) associated with the event. For a closer look at these DTOs, refer to the data model.
  • Order: The order in which business rules are executed when triggered by the same event.
  • Execution:
    • Asynchronous (occurs after synchronization with client application).
    • Synchronous (occurs during synchronization with client application). Synchronous business rules are used in combination with a validation action and can cause delays in synchronization with client apps (for example, mobile).
  • Delay Execution (Sec): You can define a business rule to be executed with a delay of up to 600 seconds. Only business rules with the execution type "asynchronous" can be delayed, excluding scheduled business rules. Please consider the following before using a delay:
    • A business rule will only be executed once the duration of a delay is reached.
    • As the delay only defines the minimum duration that needs to be reached, it can be that the asynchronous execution of a business rule is further delayed than defined.
    • If there are business rules triggered by the same event (that is, the update of an activity) and defined with different delays, it is not guaranteed that the order of execution will be based on the duration of the delays defined.
    • To ensure that business rules are executed in a specific order, a better alternative is to use the order field.
    • If you combine using a delay and order in business rules triggered by the same event, the order will only be considered for those rules defined with the same delay. This ensures these rules are triggered at the same point in time and then the correct order of the rules can be considered. The system is not able to consider the order of rules triggered at different times due to different delays.
    • In general it is recommend to use delays cautiously. Delayed executions put an additional load on the system, which can have an impact on system performance.
  • CoreSQL WHERE Clause: This field will appear when the event type is "scheduled". It is recommended to use this field to determine the objects for which the rule should be executed for. For each object record found, the variables and conditions will be resolved and the actions executed accordingly.
  • Frequency: This field will appear when the event type is "scheduled". Here, you can enter the frequency at which the business rule should be triggered when the conditions are met.

Scheduled Business Rules

Time of triggered action: The system allows you to trigger a scheduled business rule up to 4 times an hour (XX:00, XX:15, XX:30, XX:45).

Note

The rule is triggered at the exact given time, however the execution might be delayed by several minutes.

The execution of business rules is optimized to best preserve the server's resources (CPU, Memory, and so on).

Frequency and execution time: Depending on the subset of data to be processed and the actions defined in a business rule, the execution time varies from one rule to another. In order to protect the resources of the cloud system, the execution of a business rule might get skipped. This can happen when the same rule is triggered for execution while the previous rule execution is still running.

To avoid this, the business rule schedule must be configured so that the [TriggerTime]+[ExecutionTime] does not meet the next [TriggerTime].

To avoid the defined actions not being executed because of a skipped business rule, it is strongly recommend to define the trigger conditions in such a way that the trigger conditions cannot be influenced by another process. In other words, the trigger conditions must be met at any given time as long as the desired actions of the business rules are not executed.

If a frequency is set for a business rule with a condition, it will run at the stated frequency when the rule is triggered.

Usage of variables in a business rule.

Variables are used to read data that is needed for the proper execution of the business rule. Data in variables can be used in subsequent Variables, Conditions, and Actions.

A variable consists of a query in CoreSQL (same as Query API), Javascript functions, or a combination thereof. The values of the variables are resolved during rule validation and execution. After a variable has been defined resolved, the variable and its properties can be referred to by using javascript. For example ${activity.code} refers to the "code" field of a previously defined variable called "activity".

Some variables come predefined and refer to the following:

  • The relevant triggering object
  • ‘Old’ vs. ‘new’ object data
  • The current user, Company, and Account

The company settings and the user settings are accessed through the variables ${company.settings} and ${user.settings}. For example, to retrieve the language that the user set in the web application, use ${user.settings.Cockpit_SelectedLanguage.data} .

Custom variables can be defined as the following:

  • Object: a single record of a given type
  • Array: allows the selection of many individual objects by using a complex query
  • Value: usually consisting of a hard coded value/text string or otherwise defined by a javascript function that is resolved during execution.
Properties or definition of a variable in a business rule.

Object- and Array Variables are specified by the following:

  • Object (DTO) Type
  • Object Version
  • CoreSQL WHERE clause or a complete CoreSQL query.

In advanced query mode, complex CoreSQL queries can be defined. To access advanced mode, press the wrench icon. When using advanced mode, all object types and their versions have to be explicitly defined, for example Activity.40;ServiceCall.26. All defined queries can be inspected and executed in the Query API interface by using the triangle icon.

The CoreSQL query or -WHERE clause can contain javascript code snippets, contained between brackets: ${ ... }

Value Variables can hold, for example a text string, a Javascript code snippet, or similar.

Custom objects and fields (also known as user-defined objects and fields) are supported. They can be referenced in variables (using the WHERE clause), conditions, and actions for all available objects.

Usage of conditions in a business rule.

Conditions are defined with built-in relational- and logical Operators and can compare the following:

  • Variables and their properties
  • Text strings
  • Numbers
  • Boolean values
  • JavaScript functions

Conditions are evaluated after resolution of the Variables. Then, if all the Conditions are met, the Actions get executed. If, however, one of the Conditions is not met, the execution of the business rule is skipped.

Skipping a lot of business rule executions still costs system resources, because the BR still reacted to the event/trigger, resolved the variables, and checked the conditions.

Usage of actions in a business rule.

A business rule can execute up to 100 actions which are executed in sequence if the business rule conditions are met. There are many different types of actions, each with their own specific input parameters. The input parameters are filled with data from the variables, JavaScript functions, hard coded values, and so on.

Examples of actions in a business rule.

Some action examples are listed below.

  • Webhook/FSM webhook: These actions return a response variable, which can be used in subsequent actions.
  • Execute Conditional Operation: Consists of a condition that determines whether the subsequent action gets executed or skipped.
  • Create/Update/Delete Object: Custom objects are supported and referenced in the object type field.
  • Build Checklist Report: For every closed checklist instance on the selected activity, the application will generate a report and attach it to the activity.
  • Validate: Used in synchronous business rules to block something from happening before committing the corresponding data to the database. For example, a mobile/web client synchronizing some new or changed data.

FSM webhook actions are limited to APIs that require FSM authentication. An advantage of this action is that you no longer need to define an explicit webhook action for determining an authorization token for OAuth2. You only need to enter the client ID and secret to ensure the proper credentials are used.

Usage of code in a business rule, including various examples.

In the Variable section, the code will consist mainly of CoreSQL, in order to gather the relevant data. Throughout the business rule, including inside the CoreSQL queries, Javascript can be used: either independently or to reference and manipulate data contained in previously-defined Variables. Everything between a dollar-sign and brackets is interpreted as JavaScript ${..}. The JavaScript functions should be based on the ECMAScript 6.0 standard.

Testing and Validation

To help ensure that the business rule will work as expected, it is necessary to validate it.

After having completed the trigger and action sections of the business rule, you can choose Save. The application only validates for required fields, so a successfully-saved business rule does not automatically indicate that the business rule is valid. However, the record will be saved and you can modify the business rule as needed until it is valid.

To help ensure that the business rule will work as expected, it is necessary to validate it. The validation function allows you to simulate the business rule execution and see how the variables are being resolved. Also, the validation function checks whether the basic syntax of your rule is correct. However, any API calls are not executed and results are not committed to the database.

To go to the Validation screen, choose Validate. On the validation screen, you need to specify any necessary input data. Usually, this means selecting a relevant object record from the database. A pop-up supports the user in selecting an object by showing a list of possible object IDs. However, it is usually necessary to first find a relevant object with Query API, so that the relevant properties are known and the business rule behavior can be simulated accordingly. The person field can be used to assess the impact of the user's permissions, or when the user's settings and other data are relevant.

During validation, all variables in the business rule will be resolved and will be shown on screen.

When the necessary data has been entered, choose Validate. During validation, all variables in the business rule will be resolved and will be shown on screen. The JavaScript snippets will be executed and replaced by their results/output. Errors lead to an interruption of the process and will be shown at the top of the screen.

A business rule can be manually executed, for example for testing purposes.

After successful validation, the next step is to execute the business rule manually, in a test environment. To go to the manual execution screen, choose Execute.

As during validation, you need to select the relevant input data. When this is done, choose Execute.

Contrary to validation, the manual execution function doesn't show the resolved variables. In addition, all actions are effectively executed and results are committed to the database. Depending on your specific business rule, the execution can be evaluated by checking the execution log, the database, or on the web UI.

After the business rule was successfully tested in manual mode, you can enable the business rule. This makes the business rule run completely automatically, according to the trigger settings.

How to Use Business Rules

Monitoring and Best Practices

The Maintenance side bar within the business rule detail screen shows execution information and contains a link to the Execution Log.

The Maintenance side bar within the business rule detail screen shows execution information from the last three months. This includes a detailed execution log with information about successful, skipped, and failed executions. Additionally, it shows the triggering object ID as well as the client and user. This data provides valuable input for debugging or troubleshooting purposes.

Business rules can be monitored on account level, using several charts, lists, and statistics.

To avoid having to access the execution log of each business rule individually, administrators can use the Business Rule Execution Details and Business Rule Jobs tabs. These can be found on account level and contain statistics about business rule executions for all companies within the account.​

The information presented on these screens comes in the form of graphs and lists, detailing business rule execution counts and execution times. The graphs and lists can be filtered to zoom into the data and time frames of interest. Failed executions are flagged or highlighted so that administrators can quickly identify problematic rules.

Rule Executions May Fail Due to Some System Exceptions

  • For example:
    • Lock issues
    • Database connection issues
    • Bad data or bad business rule configuration
  • For certain error types, business rule engine will retry:
    • Up to 4 times
    • With a delay of 5 seconds between retries
  • If the execution still fails after the retry attempts, an erroneous execution will be logged.
  • Applies to asynchronous and scheduled rules. Synchronous rules will not be re-executed.

Business rules can fail due to various reasons. These reasons can include database issues, bad data, or bad business rule configurations. In case of locked objects or failed connections, the system will try to re-execute the business rule for a certain amount of times and with certain intervals. The retry and fallback mechanisms vary depending on the error type and business rule settings. Where possible, the business rule execution log will provide details of the error cause.

Consider System Load

There is a danger of placing an excessive load on the system, thereby significantly reducing the performance of your system.

To mitigate that risk, please note the following:

  • Use the monitoring capabilities to check runtime, error rates, and general health of your business rule executions:
    • Maintenance section of the business rule detail page
    • Business rule executions tab
    • Business rule job tab
  • As a rule of thumb, the runtime of scheduled rules should not take longer than a few minutes.
  • Skipping a lot of business rule executions still costs system resources, because the BR still reacted to the event/trigger, resolved the variables, and checked the conditions.

When designing and building business rules, it is important to consider the implications for system performance and server load. After having built the rules, it is important to regularly check for issues using the provided monitoring tools. As a rule of thumb, the runtime of scheduled rules should not take longer than a few minutes.

A check is in place to prevent execution of performance-critical rules. Rules with ≥100 variables, ≥100 actions, and/or ≥100 conditions are flagged as such. The execution of a job will be discontinued in case it exceeds a time slot of 4 hours.

Consider System Load

Configure your business rules so that:
  • Queries (select statements or where clauses) limits the data to be processed to the necessary minimum.
  • Actions are of the minimum required complexity.
For event-triggered BRs:
  • Try to combine multiple BRs into a single one if they share the same trigger.
  • Carefully design your business rules, especially event triggering ones, to reduce the amount of skipping.
  • For scheduled BRs: move the conditions as much as possible to the WHERE clause.

Especially consider the collection of best practices as described in the SAP Help documentation.

Following the best practices, as described in the Help documentation, will help in ensuring that the system load is kept to a minimum and that performance remains optimal. Please consider the following main principles:

  • Configure your business rules so that queries (select statements or where clauses) limits the output data to the necessary minimum and so that actions are of the minimum required complexity.
  • For Event-triggered BRs, try to combine multiple BRs into a single one, if they share the same trigger. Carefully design your business rules, especially event triggering ones, to reduce the amount of skipping.
  • For Scheduled BRs: move the conditions as much as possible to the WHERE clause at the top of the trigger section.

Other Best Practices

Other Best Practices are:

Do not try to build (complete) integration with BRs.

Use dedicated platforms instead.

Due to limitations regarding lack of management options, such as processing queues.

Use webhook actions instead of update object actions
  • The latter are direct updates, without consistency checks.
  • Field Service Management APIs like service API do include consistency checks.
Avoid using delays. This is because:
  • This blocks system resources.
  • Risk of data inconsistencies.
  • Risk of imprecise delays.
Especially consider the collection of best practices as described in the SAP Help documentation.

Do not try to build or replace (complete) integration with business rules (use dedicated platforms instead). This is generally not recommended, because of the lack of a processing queue, limited payload information, and limited retry mechanisms.

Use webhook actions instead of update object actions. In general it's better to use webhook actions (they call defined API endpoints) because update object actions are just technical updates which often are executed without consistency or business checks. In addition, the documented specific APIs, such as service API, include consistency checks.

Delays should be avoided for several reasons, as listed below:

  • They block system resources.
  • There is a risk of data inconsistencies. The complete execution of the BR is put on hold during the delay-time frame, and also the selection of variables is delayed, except for the default variables. The default variables keep their values during the delay time-frame. For example, if the BR is triggered by "on object update" with the object type "activity", then the variables ${activity}, ${new}, and ${old} will not have any changed values due to the delay. After the delay time is met, the complete execution process of the rule is carried out, which includes the evaluation of variables, conditions, and then execution of the actions.
  • There is a risk of imprecise delays. Delayed rules are entered into a table and every 60 seconds a job runs and executes all the rules where the delay condition is met. In theory it could happen that if the job just started and the rule with a 2 second delay just got triggered, that delayed rule would have to wait 60 seconds until the next job starts. This is why it could be that you see a delay longer than what is specified in the rule.

Challenge Question

Challenge Yourself: Putting Your Knowledge to the Test

In this lesson, you'll have the opportunity to apply the concepts and knowledge you've gained throughout the unit. We've designed an engaging Challenge Question that will put your critical thinking skills to work. Take a moment to reflect on what you've learned, and then use that understanding to craft your own unique solution to the question at hand.

To make the most of this exercise, we encourage you to write down your answer on a separate piece of paper. This will help you organize your thoughts and measure your learning progress. When you've completed your answer, compare it to the expert response provided. This will give you valuable insight into how well you've grasped the material and where you might need to focus your attention for further growth.

Remember, this is an opportunity to apply your understanding in a practical way, so don't hesitate to think creatively and explore different approaches. Your active participation in this lesson will reinforce your learning and prepare you for success in the real world.

Scenario:

You are a Field Service Management (FSM) consultant working with a company that provides maintenance services for industrial equipment. The company wants to automate their process of assigning follow-up activities to technicians after a service call is completed.

They also want to send an SMS notification to the customer when the follow-up activity is scheduled. Your task is to design a Business Rule in FSM that will:

1. Automatically create a follow-up activity when a service call is completed.

2. Assign the follow-up activity to the same technician who completed the service call.

3. Send an SMS notification to the customer with the details of the scheduled follow-up activity.

Consider the following while designing your Business Rule: - The main settings, trigger, and action sections of the Business Rule. - The type of Business Rule to use (Type Two or Type Three). - The use of CoreSQL queries and JavaScript snippets to specify the actions. - The potential impact on system performance and server load.

Provide a step-by-step explanation of how you would create this Business Rule in FSM, and how you would validate and test it before deploying it in the production environment.

Expert Consultant Response

Here's a step-by-step approach to creating the Business Rule in FSM:

  1. Navigate to the Business Rules section in the FSM Company Administration page.
  2. Choose the Create button to create a new Business Rule.
  3. In the main settings section: - Provide a meaningful name and description for the Business Rule. - Select "Type Three" as the Business Rule type for better performance and flexibility. - Enable the Business Rule and set the Error Threshold Percent as needed.
  4. In the trigger section: - Select "On Object Update" as the event type. - Choose "Service Call" as the object type. - Set the execution type to "Asynchronous" to avoid delays in the mobile app. - Use a CoreSQL WHERE clause to filter the service calls that have been completed.
  5. In the Action section, add the following actions in sequence: - "Create Object" action to create a follow-up activity: - Set the object type

Lesson Recap

Recap of the most important elements of the Unit 5 lesson, linked to the objective of effectively leveraging SAP Field Service Management (FSM) through Business Rules:

1. Understanding Business Rules:

- Business Rules are custom logic pieces used to enhance the standard FSM solution for specific customer cases.

- Knowing how to create, execute, and apply Business Rules is crucial for effectively leveraging FSM capabilities.

2. Exploring Business Rule Components:

- Main settings, trigger, and action sections are the defining components of Business Rules.

- Mastering these components and their related settings and functionalities is essential for implementing Business Rules effectively.

3. Choosing the Right Business Rule Type:

- Different types of Business Rules have varying capabilities.

- Type Three rules should be the preferred choice for better performance and flexibility.

4. Specifying Actions using CoreSQL and JavaScript:

- CoreSQL queries and JavaScript snippets are used to specify the actions a Business Rule should undertake.

- Understanding how to use these tools and calculate their potential impact on system performance is crucial for creating efficient Business Rules.

5. Validating and Executing Business Rules:

- Learning the key steps in testing Business Rules, interpreting results, and rectifying errors is essential for ensuring their proper functioning.

- Knowing how to validate and execute Business Rules is critical for leveraging FSM capabilities effectively.

6. Administering Business Rules:

- Understanding how to administer Business Rules for system administration and server resource management is important for handling execution issues and monitoring rules.

- Familiarity with the tools available for monitoring Business Rules is crucial for maintaining FSM performance.

7. Applying Best Practices:

- Adopting best practices when designing, building, testing, executing, and monitoring Business Rules is essential for creating efficient and effective solutions.

- Emphasizing the relationship between CoreSQL queries, Business Rules, and their impact on system performance is crucial for optimizing FSM capabilities.

Use Business Rules

Log in to track your progress & complete quizzes