We have a requirement where in we need to import and Excel file into Project Journal using AL extensions (D365 BC SAAS model).
I researched online and found very few blog on this, hence I thought of writing it here.
codeunit 50101 "Import Excel to Job Journal"
{
trigger OnRun()
begin
ImportJobJournalExcel();
end;
procedure ImportJobJournalExcel()
begin
Rec_ExcelBuffer.DeleteAll();
Rows := 0;
Columns := 0;
DialogCaption := 'Select File to upload';
UploadResult := UploadIntoStream(DialogCaption, '', '', Name, NVInStream);
If Name <> '' then
Sheetname := Rec_ExcelBuffer.SelectSheetsNameStream(NVInStream)
else
exit;
Rec_ExcelBuffer.Reset();
Rec_ExcelBuffer.OpenBookStream(NVInStream, Sheetname);
Rec_ExcelBuffer.ReadSheet();
Commit();
//Message('sheet %1', Sheetname);
//finding total number of Rows to Import
Rec_ExcelBuffer.Reset();
Rec_ExcelBuffer.SetRange("Column No.", 1);
If Rec_ExcelBuffer.FindFirst() then
repeat
Rows := Rows + 1;
until Rec_ExcelBuffer.Next() = 0;
Message('No. of rows %1', Rows);
//Finding total number of columns to import
Rec_ExcelBuffer.Reset();
Rec_ExcelBuffer.SetRange("Row No.", 1);
if Rec_ExcelBuffer.FindFirst() then
repeat
Columns := Columns + 1;
until Rec_ExcelBuffer.Next() = 0;
//Function to Get the last line number in Job Journal
LineNo := FindLineNo();
for RowNo := 2 to Rows do begin
RecJobJnl.Init();
Evaluate(RecJobJnl."Journal Template Name", GetValueAtIndex(RowNo, 1));
Evaluate(RecJobJnl."Journal Batch Name", GetValueAtIndex(RowNo, 2));
RecJobJnl."Line No." := LineNo;
RecJobJnl.Type := RecJobJnl.Type::"G/L Account";
Evaluate(RecJobJnl."Location Code", GetValueAtIndex(RowNo, 4));
RecJobJnl.Validate(RecJobJnl."Location Code");
Evaluate(RecJobJnl."Shortcut Dimension 1 Code", GetValueAtIndex(RowNo, 5));
RecJobJnl.Validate(RecJobJnl."Shortcut Dimension 1 Code");
//Evaluate(RecJobJnl."Shortcut Dimension 3 Code", GetValueAtIndex(RowNo, 6));
Evaluate(RecJobJnl."Job No.", GetValueAtIndex(RowNo, 7));
RecJobJnl.Validate(RecJobJnl."Job No.");
Evaluate(RecJobJnl."Job Task No.", GetValueAtIndex(RowNo, 8));
RecJobJnl.Validate(RecJobJnl."Job Task No.");
Evaluate(RecJobJnl."No.", GetValueAtIndex(RowNo, 3));
RecJobJnl.Validate("No.", RecJobJnl."No.");
Evaluate(RecJobJnl."Document No.", GetValueAtIndex(RowNo, 9));
Evaluate(TxtDate, GetValueAtIndex(RowNo, 10));
//CopyStr function used to convert Document Date Text value to a Date value
Evaluate(DocumentDate, CopyStr(TxtDate, 1, 2) + CopyStr(TxtDate, 4, 2) + CopyStr(TxtDate, 7, 4));
RecJobJnl.Validate("Document Date", DocumentDate);
RecJobJnl.Validate("Posting Date", DocumentDate);
Evaluate(RecJobJnl.Description, GetValueAtIndex(RowNo, 11));
Evaluate(RecJobJnl.Quantity, GetValueAtIndex(RowNo, 12));
RecJobJnl.Validate(RecJobJnl.Quantity);
RecJobJnl.Insert();
LineNo := LineNo + 10000;
end;
Message('Import Completed');
end;
local procedure GetValueAtIndex(RowNo: Integer; ColNo: Integer): Text
var
Rec_ExcelBuffer: Record "Excel Buffer";
begin
Rec_ExcelBuffer.Reset();
If Rec_ExcelBuffer.Get(RowNo, ColNo) then
exit(Rec_ExcelBuffer."Cell Value as Text");
end;
local procedure FindLineNo(): Integer
var
JobJnl: Record "Job Journal Line";
LineNo: Integer;
begin
JobJnl.Reset();
JobJnl.SetFilter("Journal Template Name", 'JOB');
JobJnl.SetCurrentKey(JobJnl."Line No.");
If JobJnl.FindLast() then
LineNo := JobJnl."Line No." + 10000
else
LineNo := 10000;
Message('Line number %1', LineNo);
exit(LineNo);
end;
var
Rec_ExcelBuffer: Record "Excel Buffer";
Rows: Integer;
Columns: Integer;
Fileuploaded: Boolean;
UploadIntoStream: InStream;
FileName: Text;
Sheetname: Text;
UploadResult: Boolean;
DialogCaption: Text;
Name: Text;
NVInStream: InStream;
RecJobJnl: Record "Job Journal Line";
RowNo: Integer;
TxtDate: Text;
DocumentDate: Date;
LineNo: Integer;
}