Custom Rule Implementation
Overview
This documentation explains how to create and implement a custom rule for document processing, specifically focusing on assigning vendor information based on folder names. The process involves extending rule types, implementing interfaces, and handling parameters within the PIMX framework.
Key Steps
1. Rule Creation
A new rule is created to assign vendor information to documents. The condition for the rule is based on the folder name, which is generated for each document.
2. Assigning Fields
The goal is to assign values to document source and document source number fields. The vendor document source option is used, and the lookup is performed based on the folder name.
3. AL Project Setup
A suitable AL project is created using AL Go. TMX is added as a dependency in app.json, and symbols are downloaded.
4. Extending Rule Types
Extend the rule action type enumeration from PIMX. Create an enum extension and add a new value for the custom rule action.
5. Implementing the Rule Action
Define a code unit that implements the rule action interface. The code unit (Rule action assign vendor) implements the required interface functions.
6. Interface Functions Explained
Create Parameters: Define parameter names and optionally default values. Use labels for best practice. Valid Parameter Value: Validate user-entered parameter values and throw errors if invalid. Lookup Parameter Value: Provide lookup options for parameters, especially if referencing existing entities. Get Description: Assemble a description from parameter labels and values for display in the UI.
7. Execution Logic
Check source and master records. Validate parameters. Assign the required data (e.g., document source and document source number). Use fixed values or look up values based on the source record.
8. Rule Data Handling
The get value function allows retrieval of values from previous rule actions. Rule processing iterates through modified product features, applies conditions, and executes actions.
9. Limitations
When writing rules as extensions, you cannot rely on rule data; only product feature data is available.
Source Code snippets
Purpose: Adds a new rule action type called AssignVendor and links it to the implementation code unit.
1. Enum Extension for Rule Action Type
enumextension 50100 "Rule Action Type Ext" extends "PIMX Rule Action Type"
{
value(50101; Assign Vendor)
{
Caption = 'Assign Vendor';
Implementation = "PIMX Rule Action Type c1" = "Rule Action Assign Vendor";
}
}
Purpose: Adds a new rule action type called AssignVendor and links it to the implementation code unit.
2. Codeunit Implementation
codeunit 50101 "Rule Action Assign Vendor" implements "PIMX Rule Action Type c1"
{
var
Param1Lbl: Label 'Parameter 1';
Param2Lbl: Label 'Parameter 2';
Param3Lbl: Label 'Parameter 3';
Param1Err: Label 'Error on PARAMETER 1';
/// <summary>
/// This method is called to create set of parameters
/// </summary>
/// <param name = "_table"></param>
/// <param name = "_parameters"></param>
procedure CreateParameters(tablenumber: Integer; var _parameters: Dictionary of [Text, Text])
begin
_parameters.Add('Parameter 1', '');
_parameters.Add('Parameter 2', '');
_parameters.Add('Parameter 3', '');
_parameters.Add('SourceNumber', '');
end;
/// <summary>
/// This method is called to validate the value of the parameter
/// </summary>
/// <param name = "TableNO"></param>
/// <param name = "FieldNo"></param>
procedure ValidateParameterValue(_name: Text; _value: Text)
begin
if _name = this.param1Lbl then begin
Error(this.Param1Err);
end;
end;
/// <summary>
/// This method is called to lookup the value of the parameter
/// </summary>
/// <param name = "TableNO"></param>
/// <param name = "FieldNo"></param>
procedure LookupParameterValue(_rec: Record "PIMX Rule Parameters"; var _value: Text)
begin
end;
/// <summary>
/// This method is called to get the description of the parameter
/// </summary>
/// <param name = "TableNO"></param>
/// <param name = "FieldNo"></param>
procedure GetDescription(_parameters: Dictionary of [Text, Text]): Text
var
Description: Text;
begin
Description += this.Param1Lbl + ':' + _parameters.Get(this.Param1Lbl)
exit(Description);
end;
/// <summary>
/// This overload method is called to execute action defined in the rule.
/// </summary>
/// <param name = "SourceRec"></param>
/// <param name = "MastereRec"></param>
/// <param name = "Parameters"></param>
/// <param name = "Data"></param>
procedure Execute(SourceRec: Variant; MasterRec: Variant; Parameters: Dictionary of [Text, Text]; Data: Codeunit "PIMX Rule Data")
var
Vendor: Record Vendor;
Document_ Record "PIMX Document";
begin
// ...
if Vendor. FindFirst() then begin
Document."Document Source" := Document."Document Source" :: Example;
Document."Document Source No" := Vendor." No";
end;
end;
}