Setting Up and Applying Validation Rule

Executing the example requires the following objects in the repository:

Add links to the following system assemblies:

Fore Example

Create a form and add the following components to it:

Sub Button1OnClick(Sender: Object; Args: IMouseEventArgs);
Var
    Mb: IMetabase;
    RubrKey: Integer;
    ValidObj: IMetabaseObject;
    ValidFilter: IValidationFilter;
    DateSettings: IValidationDateSettings;
    FilterSett: IValidationFilterSettings;
    Level: IValidationLevel;
    ComparisonValue: IValidationComparisonValue;
    ValidExecSett: IValidationExecuteSettings;
    Analyzer: IEaxAnalyzer;
    ValidExecRun: IValidationExecRun;
    DiagRep: IDiagnosticReport;
Begin
    Mb := MetabaseClass.Active;
    // Get a validation rule
    RubrKey := Mb.GetObjectKeyById("TSDB");
    ValidObj := Mb.ItemByIdNamespace("NEWVALRULE", RubrKey).Edit;
    ValidFilter := ValidObj As IValidationFilter;
    // Set start and end dates of rule calculation
    DateSettings := ValidFilter.StartDateSettings;
    DateSettings.DateOptions := ValidationDateOptions.DependsFromData;
    DateSettings := ValidFilter.EndDateSettings;
    DateSettings.DateOptions := ValidationDateOptions.DependsFromData;
    // Set calendar level to calculate the rule
    ValidFilter.Level := DimCalendarLevel.Year;
    // Set threshold value for rule exceptions
    ValidFilter.ExceptionsLimit := 500000;
    // Set parameters for calculation of the rule
    FilterSett := ValidFilter.Settings;
    // Set the rule type
    ValidFilter.Kind := ValidationDetailsKind.Level;
    Level := ValidFilter.Details As IValidationLevel;
    // Set conditions of the rule
    ComparisonValue := Level.ComparisonValue;
    ComparisonValue.ComparisonOperator := ValidationComparisonOperator.More;
    ComparisonValue.Percentage := False;
    ComparisonValue.Value1 := 100;
    // Save changes in the rule
    ValidObj.Save;
    // Execute the rule in the loaded workbook
    ValidExecSett := New ValidationExecuteSettings.Create;
    Analyzer := UiErAnalyzer1.ErAnalyzer;
    ValidExecSett.Laner := Analyzer.Laner;
    ValidExecRun := ValidFilter.Execute(ValidExecSett);
    DiagRep := New DiagnosticReport.Create;
    DiagRep.Run := ValidExecRun;
    DiagRep.EaxAnalyzer := Analyzer;
End Sub Button1OnClick;

After executing the example, on clicking the button, values greater than 100 are highlighted in red in the table.

Fore.NET Example

Create a .NET form and add the following components to it:

Imports Prognoz.Platform.Interop.Laner;
Imports Prognoz.Platform.Interop.Cubes;
Imports Prognoz.Platform.Interop.Express;
Imports Prognoz.Platform.Interop.Dimensions;
Imports Prognoz.Platform.Interop.Ms;
Imports Prognoz.Platform.Interop.Drawing;

...

Private Sub button1_Click(sender: System.Object; e: System.EventArgs);
Var
    Mb: IMetabase;
    RubrKey: Uinteger;
    ValidObj: IMetabaseObject;
    ValidFilter: IValidationFilter;
    DateSettings: IValidationDateSettings;
    FilterSett: IValidationFilterSettings;
    Level: IValidationLevel;
    ComparisonValue: IValidationComparisonValue;
    ValidExecSett: ValidationExecuteSettings;
    Analyzer: EaxAnalyzer;
    ValidExecRun: IValidationExecRun;
    DiagRep: IDiagnosticReport;
    GxColorCls: GxColorClassClass = New GxColorClassClass();
Begin
    Mb := Self.Metabase;
    // Get a validation rule
    RubrKey := Mb.GetObjectKeyById("TSDB");
    ValidObj := Mb.ItemByIdNamespace["NEWVALRULE", RubrKey].Edit();
    ValidFilter := ValidObj As IValidationFilter;
    // Set start and end dates of rule calculation
    DateSettings := ValidFilter.StartDateSettings;
    DateSettings.DateOptions := ValidationDateOptions.vdoDependsFromData;
    DateSettings := ValidFilter.EndDateSettings;
    DateSettings.DateOptions := ValidationDateOptions.vdoDependsFromData;
    // Set calendar level to calculate the rule
    ValidFilter.Level := DimCalendarLevel.dclYear;
    // Set threshold value for rule exceptions
    ValidFilter.ExceptionsLimit := 500000;
    // Set parameters for calculation of the rule
    FilterSett := ValidFilter.Settings;
    FilterSett.FontColor := GxColorCls.FromName("Red");
    // Set the rule type
    ValidFilter.Kind := ValidationDetailsKind.vdkLevel;
    Level := ValidFilter.Details As IValidationLevel;
    // Set conditions of the rule
    ComparisonValue := Level.ComparisonValue;
    ComparisonValue.ComparisonOperator := ValidationComparisonOperator.vcoMore;
    ComparisonValue.Percentage := False;
    ComparisonValue.Value1 := 100;
    // Save changes in the rule
    ValidObj.Save();
    // Execute the rule in the loaded workbook
    ValidExecSett := New ValidationExecuteSettings.Create();
    Analyzer := uiErAnalyzerNet1.ErAnalyzer.ErAnalyzer;
    ValidExecSett.Laner := Analyzer.Laner;
    ValidExecRun := ValidFilter.Execute(ValidExecSett);
    DiagRep := New DiagnosticReport.Create();
    DiagRep.Run := ValidExecRun;
    DiagRep.EaxAnalyzer := Analyzer;
End Sub;

The result of example execution matches with that of the Fore example.

See also:

Examples | Creating a Validation Rule