codeunit 50101 "Reference Helper" { procedure IsExtensionInstalled(ExtensionName: Text; ShowError: Boolean): Boolean var App: Record "NAV App Installed App"; ErrNotInstalled: Label 'There is no Extension matching ''%1'' installed'; begin app.setfilter(Name, '@*' + ExtensionName + '*'); if app.FindFirst() then exit(true) else if ShowError then error(ErrNotInstalled, ExtensionName); end; procedure LookupCustomTable(TableName: text; FieldNameToReturn: Text[50]): Text var TableMeta: Record "Table Metadata"; PageMeta: Record "Page Metadata"; InvTypeVar: Variant; RecRef: RecordRef; DataTypeMgt: Codeunit "Data Type Management"; InvTypeRef: RecordRef; FieldMeta: Record Field; begin // If table exist (Elysys Wealth) TableMeta.SetRange(Name, TableName); if not TableMeta.FindFirst() then Error(ErrTableNotFound, TableName); // Search list page PageMeta.SetRange(SourceTable, TableMeta.ID); PageMeta.SetRange(PageType, PageMeta.PageType::List); if PageMeta.FindFirst() then begin InvTypeRef.Open(TableMeta.ID); InvTypeVar := InvTypeRef; // Run Page if Page.RunModal(PageMeta.ID, InvTypeVar) = Action::LookupOK then // Get selected record Code if DataTypeMgt.GetRecordRef(InvTypeVar, RecRef) then begin FieldMeta.setrange(TableNo, TableMeta.ID); FieldMeta.SetRange(FieldName, FieldNameToReturn); if FieldMeta.FindFirst() then exit(format(RecRef.field(FieldMeta."No.").Value())); end; end; end; procedure OpenRecRef(var RecRef: RecordRef; TableName: Text; IsTemp: Boolean; Company: Text) var TableMeta: Record "Table Metadata"; begin TableMeta.SetRange(Name, TableName); if TableMeta.FindFirst() then begin RecRef.Open(TableMeta.ID, IsTemp, Company); GlobalRecRef := RecRef; end else Error(ErrTableNotFound, TableName); end; procedure SetFilter(FieldName: Text; FieldFilter: Text) var FieldMeta: Record Field; TableMeta: Record "Table Metadata"; begin if GlobalRecRef.Number() = 0 then error(ErrRecRefNotOpen); FieldMeta.SetRange(TableNo, GlobalRecRef.Number()); FieldMeta.SetRange(FieldName, FieldName); if not FieldMeta.FindFirst() then begin TableMeta.get(GlobalRecRef.Number()); Error(ErrFieldNotFound, FieldName, TableMeta.Name); end; GlobalRecRef.Field(FieldMeta."No.").SetFilter(FieldFilter); end; procedure SetRange(FieldName: Text; FieldFilter: Variant) var FieldMeta: Record Field; TableMeta: Record "Table Metadata"; begin if GlobalRecRef.Number() = 0 then error(ErrRecRefNotOpen); FieldMeta.SetRange(TableNo, GlobalRecRef.Number()); FieldMeta.SetRange(FieldName, FieldName); if not FieldMeta.FindFirst() then begin TableMeta.get(GlobalRecRef.Number()); Error(ErrFieldNotFound, FieldName, TableMeta.Name); end; GlobalRecRef.Field(FieldMeta."No.").Setrange(FieldFilter); end; procedure SetFieldValue(FieldName: Text; FieldValue: Variant; ValidateField: Boolean) var FieldMeta: Record Field; TableMeta: Record "Table Metadata"; begin if GlobalRecRef.Number() = 0 then error(ErrRecRefNotOpen); FieldMeta.SetRange(TableNo, GlobalRecRef.Number()); FieldMeta.SetRange(FieldName, FieldName); if not FieldMeta.FindFirst() then begin TableMeta.get(GlobalRecRef.Number()); Error(ErrFieldNotFound, FieldName, TableMeta.Name); end; // Control type matching if ((FieldMeta.Type = FieldMeta.Type::Decimal) and (not FieldValue.IsDecimal) and (not FieldValue.IsInteger)) or ((FieldMeta.Type = FieldMeta.Type::Integer) and (not FieldValue.IsInteger)) or ((FieldMeta.Type = FieldMeta.Type::text) and (not FieldValue.IsText) and (not FieldValue.IsCode)) or ((FieldMeta.Type = FieldMeta.Type::code) and (not FieldValue.IsCode) and (not FieldValue.IsText)) or ((FieldMeta.Type = FieldMeta.Type::date) and (not FieldValue.IsDate)) or ((FieldMeta.Type = FieldMeta.Type::time) and (not FieldValue.IsTime)) or ((FieldMeta.Type = FieldMeta.Type::datetime) and (not FieldValue.IsDateTime)) or ((FieldMeta.Type = FieldMeta.Type::GUID) and (not FieldValue.IsGuid)) or ((FieldMeta.Type = FieldMeta.Type::RecordID) and (not FieldValue.IsRecord)) or ((FieldMeta.Type = FieldMeta.Type::Option) and (not FieldValue.IsOption)) or ((FieldMeta.Type = FieldMeta.Type::DateFormula) and (not FieldValue.IsDateFormula)) or ((FieldMeta.Type = FieldMeta.Type::Duration) and (not FieldValue.IsDuration)) or ((FieldMeta.Type = FieldMeta.Type::Binary) and (not FieldValue.IsBinary)) or ((FieldMeta.Type = FieldMeta.Type::BigInteger) and (not FieldValue.IsBigInteger)) then error(ErrDifferentType, format(FieldValue), FieldName, FieldMeta."Type Name"); if ValidateField then GlobalRecRef.field(FieldMeta."No.").Validate(FieldValue) else GlobalRecRef.field(FieldMeta."No.").Value := FieldValue; end; procedure ValidateField(FieldName: Text) var FieldMeta: Record Field; TableMeta: Record "Table Metadata"; begin if GlobalRecRef.Number() = 0 then error(ErrRecRefNotOpen); FieldMeta.SetRange(TableNo, GlobalRecRef.Number()); FieldMeta.SetRange(FieldName, FieldName); if not FieldMeta.FindFirst() then begin TableMeta.get(GlobalRecRef.Number()); Error(ErrFieldNotFound, FieldName, TableMeta.Name); end; GlobalRecRef.field(FieldMeta."No.").Validate(); end; procedure TestField(FieldName: Text) var FieldMeta: Record Field; TableMeta: Record "Table Metadata"; begin if GlobalRecRef.Number() = 0 then error(ErrRecRefNotOpen); FieldMeta.SetRange(TableNo, GlobalRecRef.Number()); FieldMeta.SetRange(FieldName, FieldName); if not FieldMeta.FindFirst() then begin TableMeta.get(GlobalRecRef.Number()); Error(ErrFieldNotFound, FieldName, TableMeta.Name); end; GlobalRecRef.field(FieldMeta."No.").TestField(); end; procedure FieldError(FieldName: Text; ErrDescription: text) var FieldMeta: Record Field; TableMeta: Record "Table Metadata"; begin if GlobalRecRef.Number() = 0 then error(ErrRecRefNotOpen); FieldMeta.SetRange(TableNo, GlobalRecRef.Number()); FieldMeta.SetRange(FieldName, FieldName); if not FieldMeta.FindFirst() then begin TableMeta.get(GlobalRecRef.Number()); Error(ErrFieldNotFound, FieldName, TableMeta.Name); end; GlobalRecRef.field(FieldMeta."No.").fielderror(ErrDescription) end; procedure GetFieldValue(FieldName: Text): Variant var FieldMeta: Record Field; TableMeta: Record "Table Metadata"; begin if GlobalRecRef.Number() = 0 then error(ErrRecRefNotOpen); FieldMeta.SetRange(TableNo, GlobalRecRef.Number()); FieldMeta.SetRange(FieldName, FieldName); if not FieldMeta.FindFirst() then begin TableMeta.get(GlobalRecRef.Number()); Error(ErrFieldNotFound, FieldName, TableMeta.Name); end; if FieldMeta.class = FieldMeta.class::FlowField then GlobalRecRef.Field(FieldMeta."No.").CalcField(); exit(GlobalRecRef.Field(FieldMeta."No.").Value); end; procedure Get(PKValue1: Variant): Boolean var RecIDText: Text; RealRecID: RecordId; begin if GlobalRecRef.Number() = 0 then error(ErrRecRefNotOpen); RecIDText := FORMAT(GlobalRecRef.Number()) + ': '; if (strpos(format(PKValue1), ',') <> 0) then RecIDText += '"' + format(PKValue1) + '"' else RecIDText += format(PKValue1); Evaluate(RealRecID, RecIDText); exit(GlobalRecRef.get(RealRecID)); end; procedure Get(PKValue1: Variant; PKValue2: Variant): Boolean var RecIDText: Text; RealRecID: RecordId; begin if GlobalRecRef.Number() = 0 then error(ErrRecRefNotOpen); RecIDText := FORMAT(GlobalRecRef.Number()) + ': '; if (strpos(format(PKValue1), ',') <> 0) then RecIDText += '"' + format(PKValue1) + '"' else RecIDText += format(PKValue1); RecIDText += ','; if (strpos(format(PKValue2), ',') <> 0) then RecIDText += '"' + format(PKValue2) + '"' else RecIDText += format(PKValue2); Evaluate(RealRecID, RecIDText); exit(GlobalRecRef.get(RealRecID)); end; procedure Get(PKValue1: Variant; PKValue2: Variant; PKValue3: Variant): Boolean var RecIDText: Text; RealRecID: RecordId; begin if GlobalRecRef.Number() = 0 then error(ErrRecRefNotOpen); RecIDText := FORMAT(GlobalRecRef.Number()) + ': '; if (strpos(format(PKValue1), ',') <> 0) then RecIDText += '"' + format(PKValue1) + '"' else RecIDText += format(PKValue1); RecIDText += ','; if (strpos(format(PKValue2), ',') <> 0) then RecIDText += '"' + format(PKValue2) + '"' else RecIDText += format(PKValue2); RecIDText += ','; if (strpos(format(PKValue3), ',') <> 0) then RecIDText += '"' + format(PKValue3) + '"' else RecIDText += format(PKValue3); Evaluate(RealRecID, RecIDText); exit(GlobalRecRef.get(RealRecID)); end; procedure Get(PKValue1: Variant; PKValue2: Variant; PKValue3: Variant; PKValue4: Variant): Boolean var RecIDText: Text; RealRecID: RecordId; begin if GlobalRecRef.Number() = 0 then error(ErrRecRefNotOpen); RecIDText := FORMAT(GlobalRecRef.Number()) + ': '; if (strpos(format(PKValue1), ',') <> 0) then RecIDText += '"' + format(PKValue1) + '"' else RecIDText += format(PKValue1); RecIDText += ','; if (strpos(format(PKValue2), ',') <> 0) then RecIDText += '"' + format(PKValue2) + '"' else RecIDText += format(PKValue2); RecIDText += ','; if (strpos(format(PKValue3), ',') <> 0) then RecIDText += '"' + format(PKValue3) + '"' else RecIDText += format(PKValue3); RecIDText += ','; if (strpos(format(PKValue4), ',') <> 0) then RecIDText += '"' + format(PKValue4) + '"' else RecIDText += format(PKValue4); Evaluate(RealRecID, RecIDText); exit(GlobalRecRef.get(RealRecID)); end; procedure Get(PKValue1: Variant; PKValue2: Variant; PKValue3: Variant; PKValue4: Variant; PKValue5: Variant): Boolean var RecIDText: Text; RealRecID: RecordId; begin if GlobalRecRef.Number() = 0 then error(ErrRecRefNotOpen); RecIDText := FORMAT(GlobalRecRef.Number()) + ': '; if (strpos(format(PKValue1), ',') <> 0) then RecIDText += '"' + format(PKValue1) + '"' else RecIDText += format(PKValue1); RecIDText += ','; if (strpos(format(PKValue2), ',') <> 0) then RecIDText += '"' + format(PKValue2) + '"' else RecIDText += format(PKValue2); RecIDText += ','; if (strpos(format(PKValue3), ',') <> 0) then RecIDText += '"' + format(PKValue3) + '"' else RecIDText += format(PKValue3); RecIDText += ','; if (strpos(format(PKValue4), ',') <> 0) then RecIDText += '"' + format(PKValue4) + '"' else RecIDText += format(PKValue4); RecIDText += ','; if (strpos(format(PKValue5), ',') <> 0) then RecIDText += '"' + format(PKValue5) + '"' else RecIDText += format(PKValue5); Evaluate(RealRecID, RecIDText); exit(GlobalRecRef.get(RealRecID)); end; procedure Get(PKValue1: Variant; PKValue2: Variant; PKValue3: Variant; PKValue4: Variant; PKValue5: Variant; PKValue6: Variant): Boolean var RecIDText: Text; RealRecID: RecordId; begin if GlobalRecRef.Number() = 0 then error(ErrRecRefNotOpen); RecIDText := FORMAT(GlobalRecRef.Number()) + ': '; if (strpos(format(PKValue1), ',') <> 0) then RecIDText += '"' + format(PKValue1) + '"' else RecIDText += format(PKValue1); RecIDText += ','; if (strpos(format(PKValue2), ',') <> 0) then RecIDText += '"' + format(PKValue2) + '"' else RecIDText += format(PKValue2); RecIDText += ','; if (strpos(format(PKValue3), ',') <> 0) then RecIDText += '"' + format(PKValue3) + '"' else RecIDText += format(PKValue3); RecIDText += ','; if (strpos(format(PKValue4), ',') <> 0) then RecIDText += '"' + format(PKValue4) + '"' else RecIDText += format(PKValue4); RecIDText += ','; if (strpos(format(PKValue5), ',') <> 0) then RecIDText += '"' + format(PKValue5) + '"' else RecIDText += format(PKValue5); RecIDText += ','; if (strpos(format(PKValue6), ',') <> 0) then RecIDText += '"' + format(PKValue6) + '"' else RecIDText += format(PKValue6); Evaluate(RealRecID, RecIDText); exit(GlobalRecRef.get(RealRecID)); end; procedure Get(PKValue1: Variant; PKValue2: Variant; PKValue3: Variant; PKValue4: Variant; PKValue5: Variant; PKValue6: Variant; PKValue7: Variant): Boolean var RecIDText: Text; RealRecID: RecordId; begin if GlobalRecRef.Number() = 0 then error(ErrRecRefNotOpen); RecIDText := FORMAT(GlobalRecRef.Number()) + ': '; if (strpos(format(PKValue1), ',') <> 0) then RecIDText += '"' + format(PKValue1) + '"' else RecIDText += format(PKValue1); RecIDText += ','; if (strpos(format(PKValue2), ',') <> 0) then RecIDText += '"' + format(PKValue2) + '"' else RecIDText += format(PKValue2); RecIDText += ','; if (strpos(format(PKValue3), ',') <> 0) then RecIDText += '"' + format(PKValue3) + '"' else RecIDText += format(PKValue3); RecIDText += ','; if (strpos(format(PKValue4), ',') <> 0) then RecIDText += '"' + format(PKValue4) + '"' else RecIDText += format(PKValue4); RecIDText += ','; if (strpos(format(PKValue5), ',') <> 0) then RecIDText += '"' + format(PKValue5) + '"' else RecIDText += format(PKValue5); RecIDText += ','; if (strpos(format(PKValue6), ',') <> 0) then RecIDText += '"' + format(PKValue6) + '"' else RecIDText += format(PKValue6); RecIDText += ','; if (strpos(format(PKValue7), ',') <> 0) then RecIDText += '"' + format(PKValue7) + '"' else RecIDText += format(PKValue7); Evaluate(RealRecID, RecIDText); exit(GlobalRecRef.get(RealRecID)); end; procedure Get(PKValue1: Variant; PKValue2: Variant; PKValue3: Variant; PKValue4: Variant; PKValue5: Variant; PKValue6: Variant; PKValue7: Variant; PKValue8: Variant): Boolean var RecIDText: Text; RealRecID: RecordId; begin if GlobalRecRef.Number() = 0 then error(ErrRecRefNotOpen); RecIDText := FORMAT(GlobalRecRef.Number()) + ': '; if (strpos(format(PKValue1), ',') <> 0) then RecIDText += '"' + format(PKValue1) + '"' else RecIDText += format(PKValue1); RecIDText += ','; if (strpos(format(PKValue2), ',') <> 0) then RecIDText += '"' + format(PKValue2) + '"' else RecIDText += format(PKValue2); RecIDText += ','; if (strpos(format(PKValue3), ',') <> 0) then RecIDText += '"' + format(PKValue3) + '"' else RecIDText += format(PKValue3); RecIDText += ','; if (strpos(format(PKValue4), ',') <> 0) then RecIDText += '"' + format(PKValue4) + '"' else RecIDText += format(PKValue4); RecIDText += ','; if (strpos(format(PKValue5), ',') <> 0) then RecIDText += '"' + format(PKValue5) + '"' else RecIDText += format(PKValue5); RecIDText += ','; if (strpos(format(PKValue6), ',') <> 0) then RecIDText += '"' + format(PKValue6) + '"' else RecIDText += format(PKValue6); RecIDText += ','; if (strpos(format(PKValue7), ',') <> 0) then RecIDText += '"' + format(PKValue7) + '"' else RecIDText += format(PKValue7); RecIDText += ','; if (strpos(format(PKValue8), ',') <> 0) then RecIDText += '"' + format(PKValue8) + '"' else RecIDText += format(PKValue8); Evaluate(RealRecID, RecIDText); exit(GlobalRecRef.get(RealRecID)); end; procedure Get(PKValue1: Variant; PKValue2: Variant; PKValue3: Variant; PKValue4: Variant; PKValue5: Variant; PKValue6: Variant; PKValue7: Variant; PKValue8: Variant; PKValue9: Variant): Boolean var RecIDText: Text; RealRecID: RecordId; begin if GlobalRecRef.Number() = 0 then error(ErrRecRefNotOpen); RecIDText := FORMAT(GlobalRecRef.Number()) + ': '; if (strpos(format(PKValue1), ',') <> 0) then RecIDText += '"' + format(PKValue1) + '"' else RecIDText += format(PKValue1); RecIDText += ','; if (strpos(format(PKValue2), ',') <> 0) then RecIDText += '"' + format(PKValue2) + '"' else RecIDText += format(PKValue2); RecIDText += ','; if (strpos(format(PKValue3), ',') <> 0) then RecIDText += '"' + format(PKValue3) + '"' else RecIDText += format(PKValue3); RecIDText += ','; if (strpos(format(PKValue4), ',') <> 0) then RecIDText += '"' + format(PKValue4) + '"' else RecIDText += format(PKValue4); RecIDText += ','; if (strpos(format(PKValue5), ',') <> 0) then RecIDText += '"' + format(PKValue5) + '"' else RecIDText += format(PKValue5); RecIDText += ','; if (strpos(format(PKValue6), ',') <> 0) then RecIDText += '"' + format(PKValue6) + '"' else RecIDText += format(PKValue6); RecIDText += ','; if (strpos(format(PKValue7), ',') <> 0) then RecIDText += '"' + format(PKValue7) + '"' else RecIDText += format(PKValue7); RecIDText += ','; if (strpos(format(PKValue8), ',') <> 0) then RecIDText += '"' + format(PKValue8) + '"' else RecIDText += format(PKValue8); RecIDText += ','; if (strpos(format(PKValue9), ',') <> 0) then RecIDText += '"' + format(PKValue9) + '"' else RecIDText += format(PKValue9); Evaluate(RealRecID, RecIDText); exit(GlobalRecRef.get(RealRecID)); end; procedure Get(PKValue1: Variant; PKValue2: Variant; PKValue3: Variant; PKValue4: Variant; PKValue5: Variant; PKValue6: Variant; PKValue7: Variant; PKValue8: Variant; PKValue9: Variant; PKValue10: Variant): Boolean var RecIDText: Text; RealRecID: RecordId; begin if GlobalRecRef.Number() = 0 then error(ErrRecRefNotOpen); RecIDText := FORMAT(GlobalRecRef.Number()) + ': '; if (strpos(format(PKValue1), ',') <> 0) then RecIDText += '"' + format(PKValue1) + '"' else RecIDText += format(PKValue1); RecIDText += ','; if (strpos(format(PKValue2), ',') <> 0) then RecIDText += '"' + format(PKValue2) + '"' else RecIDText += format(PKValue2); RecIDText += ','; if (strpos(format(PKValue3), ',') <> 0) then RecIDText += '"' + format(PKValue3) + '"' else RecIDText += format(PKValue3); RecIDText += ','; if (strpos(format(PKValue4), ',') <> 0) then RecIDText += '"' + format(PKValue4) + '"' else RecIDText += format(PKValue4); RecIDText += ','; if (strpos(format(PKValue5), ',') <> 0) then RecIDText += '"' + format(PKValue5) + '"' else RecIDText += format(PKValue5); RecIDText += ','; if (strpos(format(PKValue6), ',') <> 0) then RecIDText += '"' + format(PKValue6) + '"' else RecIDText += format(PKValue6); RecIDText += ','; if (strpos(format(PKValue7), ',') <> 0) then RecIDText += '"' + format(PKValue7) + '"' else RecIDText += format(PKValue7); RecIDText += ','; if (strpos(format(PKValue8), ',') <> 0) then RecIDText += '"' + format(PKValue8) + '"' else RecIDText += format(PKValue8); RecIDText += ','; if (strpos(format(PKValue9), ',') <> 0) then RecIDText += '"' + format(PKValue9) + '"' else RecIDText += format(PKValue9); RecIDText += ','; if (strpos(format(PKValue10), ',') <> 0) then RecIDText += '"' + format(PKValue10) + '"' else RecIDText += format(PKValue10); Evaluate(RealRecID, RecIDText); exit(GlobalRecRef.get(RealRecID)); end; var ErrTableNotFound: Label 'The table ''%1'' could not be found in the system. Please verify that required extensions are installed correctly'; ErrFieldNotFound: Label 'The field ''%1'' could not be found in table ''%2'''; ErrRecRefNotOpen: Label 'The record reference is not open. This is a programming error'; ErrDifferentType: Label 'Unable to affect %1 in field ''%2'' because it does not match the field type %3. This is a programming error'; GlobalRecRef: RecordRef; }