Skip to main content
Skip table of contents

Examples of Well-Formatted Rules

Any custom rule or code should be written in a way that makes it easy for other people to understand it. While there are no universal styles on how to write code, there are basic guidelines that make it easier for others to understand and debug.


Example of SQL Code

Example of SQL Code

SQL
DECLARE
    --INPUT
    v_TaskId NVARCHAR2(255);
     
    --INTERNAL
    v_ignore INTEGER;
    v_CaseId INTEGER;
    v_CBD NVARCHAR2(255);
    v_ResolutionCode NVARCHAR2(255);
    v_ResolutionId INTEGER;
    v_targetStateCode NVARCHAR2(255);
     
    --LOGGING
    v_message NCLOB;
     
    --temp variables for returns
    v_tempErrMsg NCLOB;
    v_tempErrCd INTEGER;
    v_tempSccss NCLOB;
BEGIN
    --GET CASE ID
    v_Taskid := :TaskId;
    v_CaseId := f_DCM_getCaseIdByTaskId(v_Taskid);
     
    --GET CRN DATA
    BEGIN
        SELECT COL_CDB
        INTO   v_CBD
        FROM   TBL_CDM_CS_CASE_DATA_MODEL
        WHERE  COL_CS_CASE_DATA_MODELCASE = v_CaseId;
    EXCEPTION
    WHEN OTHERS THEN
        v_CBD := NULL; 
    END;
    v_message := f_UTIL_addToMessage(originalMsg => v_message, newMsg => 'CBD values was ' || NVL(v_CBD, '-NULL-'));
     
    --CALCULATE CORRECT RESOLUTION ID
    BEGIN
        IF v_CBD IS NULL THEN
            v_ResolutionCode := 'TASK_MOVE_TO_IDENTIFY';
        ELSE
            v_ResolutionCode := 'TASK_MOVE_TO_INVESTIGATE';
        END IF;
         
        SELECT COL_ID
        INTO   v_ResolutionId
        FROM   TBL_STP_RESOLUTIONCODE
        WHERE  UPPER(COL_CODE) = v_ResolutionCode;
    EXCEPTION
    WHEN OTHERS THEN
        v_tempErrCd := 101;
        v_tempErrMsg := 'Could not find Resolution Code with code ' || v_ResolutionCode;
        GOTO cleanup;
    END;
    v_message := f_UTIL_addToMessage(originalMsg => v_message, newMsg => 'Resolution ID for  ' || v_ResolutionCode || ' is ' || TO_CHAR(v_ResolutionId));
     
    --ROUTE TASK TO NEW STATE
    v_targetStateCode := 'root_TSK_Status_CLOSED_DEFAULT';
    v_ignore := f_DCM_taskTransitionManualFn(CUSTOMDATA => null,
                                             ErrorCode => v_tempErrCd,
                                             ErrorMessage => v_tempErrMsg,
                                             ResolutionId => v_ResolutionId,
                                             Target => v_targetStateCode,
                                             TaskId => v_Taskid,
                                             WorkbasketId => null);
     
     
    IF v_tempErrCd > 0 THEN
        GOTO cleanup;
    ELSE
        v_message := f_UTIL_addToMessage(originalMsg => v_message, newMsg => 'SUCCESS: routed Task to new state');
    END IF;
     
    --CREATE HISTORY RECORD FOR SUCCESS
    v_ignore := f_HIST_createHistoryFn(
        AdditionalInfo => v_message, 
        IsSystem=>0,
        Message=> 'Succesfully routed Task with ' || 'CUST_routeByCBD',
        MessageCode => NULL,
        TargetID => v_Taskid,
        TargetType=>'TASK'
    ); 
     
    --RETURN RESULTS
    :ERRORCODE := 0;
    :ERRORMESSAGE := NULL;
    :SUCCESSRESPONSE := v_message; 
    RETURN;
     
     
    --ERROR BLOCK
    <<cleanup>> 
    v_message := f_UTIL_addToMessage(originalMsg => v_message, newMsg => 'WARNING: Issue routing Task');
    v_message := f_UTIL_addToMessage(originalMsg => v_message, newMsg => 'ERROR CODE: ' || v_tempErrCd);
    v_message := f_UTIL_addToMessage(originalMsg => v_message, newMsg => 'ERROR MESSAGE: ' || v_tempErrMsg);
     
    v_ignore := f_HIST_createHistoryFn(
        AdditionalInfo => v_message, 
        IsSystem=>0,
        Message=> NULL,
        MessageCode => 'GenericEventFailure',
        TargetID => v_Taskid,
        TargetType=>'TASK'
    ); 
     
    :ERRORCODE := v_tempErrCd;
    :ERRORMESSAGE := v_message;
    :SUCCESSRESPONSE := NULL;  
 
    RETURN;
END;


Example of C# Code

Example of C# Code

C#
//------------------------------------Start rule ------------------------------------
var appToken = Convert.ToString(request["token"]);
var appDomain = Convert.ToString(request["domain"]);
 
//LOGGING HELPERS
List<String> processLog = new List<String>();
List<String> errorLog = new List<String>();
Func<string, string, Boolean> CUST_addToLog = (message, additionalInfo) =>
{
    try
    {
        var logParams = new ParameterCollection();
        logParams.AddParameter(new Parameter { Type = ParameterType.Text(), Name = "MESSAGE", Value = message });
        logParams.AddParameter(new Parameter { Type = ParameterType.Text(), Name = "ADDITIONALINFO", Value = additionalInfo });
        ASF.CoreLib.APIHelper.BDSExecute(new ASF.BDS.WebService.Messages.ExecuteRequest
        {
            Parameters = logParams,
            Domain = appDomain,
            Token = appToken,
            DataCommand = "ROOT_CUST_CREATELOGRECORD",  // This rule needs to be added to your solution in order to work
            VersionCode = null
        });
        return true;
    }
    catch (Exception ex)
    {
        errorLog.Add("ERROR adding record to log");
        errorLog.Add(ex.ToString());                   
        return false;
    }
};
processLog.Add("======RULE LOGGING BEGINS======");
 
/*==INPUT PARAMETERS==*/
Int32 v_TASKID = Int32.Parse(Convert.ToString(request["TaskId"]));
if(v_TASKID > 0)
{
    processLog.Add("Input parameter TaskId = " + v_TASKID.ToString());
} else
{
    errorLog.Add("ERROR: " + "TaskId input paramter is empty");
    goto ErrorBlock;
}
 
/*==ROUTE TASK==*/
processLog.Add("Routing Task through a SQL rule");
const string routeTaskRuleCode = "ROOT_CUST_ROUTEBYCBD"; // This rule needs to be added to your solution in order to work
var taskRouteParams = new ASF.Framework.Service.Parameters.ParameterCollection();
taskRouteParams.AddParameter(new Parameter
{
    Type = ASF.Framework.Service.Parameters.ParameterType.Integer(),
    Name = "TaskId",
    Value = v_TASKID
});
try
{
    var taskRouteResult =  ASF.CoreLib.APIHelper.BDSExecute(new ASF.BDS.WebService.Messages.ExecuteRequest
    {
        Parameters = taskRouteParams,
        Domain = appDomain,
        Token = appToken,
        DataCommand = routeTaskRuleCode
    });
    processLog.Add("Rule " + routeTaskRuleCode + " executed");
    Newtonsoft.Json.Linq.JToken taskInfoData = Newtonsoft.Json.Linq.JObject.Parse(taskRouteResult.Data.ToJson())["DATA"][routeTaskRuleCode];
    Int32 v_ERRORCODE = Convert.ToInt32((string)taskInfoData.SelectToken("ERRORCODE"));
    if (v_ERRORCODE > 0)
    {
        errorLog.Add("ERROR: " + "Issue routing Task");
        errorLog.Add("ERRORCODE: " + v_ERRORCODE.ToString());
        errorLog.Add("ERRORMESSAGE: " + (string)taskInfoData.SelectToken("ERRORMESSAGE"));
        goto ErrorBlock;
    }
    else
    {
        processLog.Add("SUCCESSRESPONSE: " + (string)taskInfoData.SelectToken("SUCCESSRESPONSE"));
    }
 
}
catch (Exception ex)
{
    errorLog.Add("ERROR: Executing rule " + routeTaskRuleCode);
    errorLog.Add("ERROR: " + ex.ToString());
    goto ErrorBlock;
}
 
/*==MAKE PROPER RESPONSE AND RETURN==*/
response["ERRORCODE"] = 0;
response["ERRORMESSAGE"] = "";
response["SUCCESSRESPONSE"] = String.Join("<br>", processLog);
 
//------------------------------------major error block------------------------------------
ErrorBlock:
    CUST_addToLog("ERROR", "<code>" + String.Join("<br>", processLog) + "<br>" + String.Join("<br>", errorLog) + "</code>");
    response["ERRORCODE"] = 101;
    response["ERRORMESSAGE"] = String.Join("<br>", processLog) + "<br>" + String.Join("<br>", errorLog);
    response["SUCCESSRESPONSE"] = "";
 
//------------------------------------End rule ------------------------------------

Predefined Sample Rules

You can find dozens of pre-built rules in AppBase that you can use as a template or modify. These rules are named with the QA_ prefix. It is recommended that you clone the rule before making any changes.

There is another set of pre-built rules that you can use as an example, those rules are named with the prefix SMPL_.


JavaScript errors detected

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

If this problem persists, please contact our support.