Skip to main content
Skip table of contents

16. Creating a Rule

The following exercise will demonstrate how to create rules based on different use cases.

AppBase uses a prefix for the rules to identify the context of execution. For example, DCM, DICT for dictionary, EMAIL, GEN for Genesys, MDM, PPL for people (users, roles, teams, etc.). AppBase also has a set of prebuilt rules intended to be used as examples. Those rules use the prefix SMPL or QA.

As a good practice, you should use the prefix CUST (custom) to identify rules created as part of a custom solution (like the Auto Loan).

In our practice, we are going to create a new rule to populate the FICO score field using a randomly generated number, and we'll use de prefix TRN for training.


Steps

  1. Navigate to Application Studio (1) → DCM Training (2) solution.
  2. Navigate to Business Rules → Rules (1)
  3. Filter the list by the Name 'QA_actionNqFn' (2)
  4. Open the sample rule QA_actionNqFn (3) by clicking the hyperlink.

  5. Click on the Clone button.

  6. In the pop-up windows, insert 'CUST_TRN_getCreditScoreFn' for the New Name of the cloned rule, then click on the Clone button.

  7. Edit the new rule by clicking the pencil button.
  8. Remove some Tags from the list that we will not need for this rule. Keep only cust and Common Event, as in the image below.

    Tags are used to filter the use of the rules. In our example, we will use this rule in Common Events, and we identify the rule as custom.


  9. For Description, enter 'GBank - Generate Random Credit Score and Update Auto Loan Application.'
  10. Keep the other options as is.
  11. In the Code Body section, paste the following SQL code, or you can download the code from this link CUST_TRN_getCreditScoreFn.sql

    CUST_TRN_getCreditScoreFn Code

    SQL
    /*
    Last Updated SEP 27, 2021
    
    Rule Name CUST_TRN_getCreditScoreFn
    Tags cust, Common Event
    Template Type Empty
    Description GBank - Generate Random Credit Score and Update Auto Loan Application.
    Rule Type SQL Non Query
    Rule must be 
    deployed to Procedure/Function
    Encoder Default
    
    Sample of simple rule that deployed as NonQuery SQL Function.
    
    Input:
    - CaseId, Number (Case.Id)
    - TaskId, Number (Task.Id)
    - ProcedureId, Number (Procedure.Id)
    - Input, Text Area (collection of passed parameters in XML format)
    - InData, Text Area (custom user input data in XML format)
    
    Output
    - ValidationResult, Integer (0 = don't allow, 1 = allow)
    - ErrorMessage, Text Area
    - ErrorCode, Number
    - OutData, Text Area (custom user output data in XML format)
    
    See results in:
    1. Case history
    2. Task history
    3. Setup -> Monitoring -> System Monitor
    */
    
    DECLARE
    v_caseId NUMBER;
    v_taskId NUMBER;
    v_procedureId NUMBER;
    v_ErrorCode NUMBER;
    v_ErrorMessage NCLOB;
    v_validationresult NUMBER;
    v_input NCLOB;
    v_InData NCLOB;
    v_outData NCLOB;
    v_res NUMBER;
    v_logId NUMBER;
    v_UCode NVARCHAR2(255);
    
    BEGIN
    --INPUT
    v_caseId := :CaseId;
    v_taskId := :TaskId;
    v_procedureId := :ProcedureId;
    v_input := :Input;
    v_InData := :InData;
    
    --INIT
    v_outData := NULL;
    v_ErrorCode := 0;
    v_ErrorMessage := NULL;
    v_validationresult := 1; --valid by default
    
    -- GENERATE RANDOM SCORE
    begin
    select TRUNC(DBMS_RANDOM.VALUE(550, 850)) into v_res from dual;
    exception
    WHEN NO_DATA_FOUND THEN
    v_res := 777;
    end;
    
    -- UPDATE CREDIT SCORE IN MDM TABLE
    begin
    UPDATE TBL_CDM_AUTO_LOAN 
    SET COL_FICO = v_res
    WHERE COL_CDM_AUTO_LOANCASE = V_CASEID;
    v_outData := 'FICO Score Updated to ' || V_RES || ' in Case #' || V_CASEID;
    exception
    WHEN OTHERS THEN
    v_ErrorMessage := '[CUST_TRN_getCreditScoreFn] Error Updating FICO Score to ' || v_res || ' in Case #' || v_caseId;
    end;
    
    
    --WRITE HISTORY AND PROCESS ERRORS
    v_res := f_HIST_createHistoryFn(
    AdditionalInfo=> 'Rule : CUST_TRN_getCreditScoreFn', 
    IsSystem => 0, 
    Message => v_outData,
    MessageCode => 'CaseUpdated', 
    TargetID => v_CaseId,
    TargetType => 'CASE'
    );
    
    :ErrorCode := v_ErrorCode;
    :ErrorMessage := v_ErrorMessage;
    :validationResult := v_validationresult;
    :OUTDATA := v_outData;
    END;
    
    
  12. Your code body should look like the following image.

  13. Validate that the names of the table and columns highlighted correspond to your Auto Loan MDM model. Focus on the following code section (around lines # 73-82). If you have followed the instructions, you should not have issues.
    Ask the instructor if you are not sure how to validate the name.

    SQL
    UPDATE TBL_CDM_AUTO_LOAN 
    SET COL_FICO = v_res
    WHERE COL_CDM_AUTO_LOANCASE = V_CASEID;
  14. Save the rule.

  15. You will see the rule summary after saving.

  16. Debug the new rule to validate that is working correctly and there are no errors by clicking the Debug button. This will open a new tab to enter values for the parameters, execute the rule, and see the results in JSON format.

  17. Test it by clicking the Run button. If there are no errors, you will have a response like the following. Notice the OUTDATA message: "FICO Score Updated to 999 in Case #".

    JS
    {
    "DATA": {
    "ROOT_CUST_TRN_GETCREDITSCOREFN": {
    "RETVAL": "-1",
    "ERRORMESSAGE": null,
    "OUTDATA": "FICO Score Updated to 999 in Case #",
    "VALIDATIONRESULT": "1",
    "ERRORCODE": "0",
    "ValidationSummary": {
    "VALIDATION": null,
    "IsValid": true
    }
    }
    },
    "RequestId": "480D3C58A7E440FC9363C16AB3C96D73"
    }
  18. Close the Debug tab by clicking the Go Back button.

  19. Close the rule tab.
  20. Deploy the rule (5. Deploying a Solution).

Next Steps

17. Registering a Business Rule

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.