Add elements to array in a loop.

mysamza
Member Posts: 66
On an Item Card Page, there is a selection of Item Category which has a Code field.
I want to print that value for each of the Items selected in the Sales Quote page (Subform, line items area)
So in Sales Quote I have selected 2 items;
1) Front Hub
2) Bicycle
and these two items have a Item Category Code set (lets say Chair, Desk respectively) in their Item Card Page. I will print Chair / Desk in the report of Sales Quote.
I am approaching this be first finding Item Category Codes for the Line Items and then putting them in an array, then I will compare and move distinct ones (reason for finding distinct is if 2 items have same Item Category Codes I have to print that Code ONLY once, so if 3 Line Items have Desk, Desk, Chair, I will print on the report Desk / Chair) into another array then concatenate the values with ' / ' into a Text Variable.
Here is what I have done so far but got stuck in getting right values into my array.
On the OnAfterGetRecord Trigger of my Sales Quote Report
I have written;
ItemCateTableRec.Reset();
ItemCateTableRec.SetFilter(Code, "Sales Line"."Item Category Code");
IF ItemCateTableRec.FindSet then begin
repeat
myArray [ i ] := ItemCateTableRec.Code;
i := i + 1;
until ItemCateTableRec.Next = 0;
end;
upon printing the myArray[1] and [2] I do not get the correct Item Category Code Chair or my Item FrontHub and Desk for my Item Bicycle. I get incorrect values.
Thanks a ton in advance for any guidance.
I want to print that value for each of the Items selected in the Sales Quote page (Subform, line items area)
So in Sales Quote I have selected 2 items;
1) Front Hub
2) Bicycle
and these two items have a Item Category Code set (lets say Chair, Desk respectively) in their Item Card Page. I will print Chair / Desk in the report of Sales Quote.
I am approaching this be first finding Item Category Codes for the Line Items and then putting them in an array, then I will compare and move distinct ones (reason for finding distinct is if 2 items have same Item Category Codes I have to print that Code ONLY once, so if 3 Line Items have Desk, Desk, Chair, I will print on the report Desk / Chair) into another array then concatenate the values with ' / ' into a Text Variable.
Here is what I have done so far but got stuck in getting right values into my array.
On the OnAfterGetRecord Trigger of my Sales Quote Report
I have written;
ItemCateTableRec.Reset();
ItemCateTableRec.SetFilter(Code, "Sales Line"."Item Category Code");
IF ItemCateTableRec.FindSet then begin
repeat
myArray [ i ] := ItemCateTableRec.Code;
i := i + 1;
until ItemCateTableRec.Next = 0;
end;
upon printing the myArray[1] and [2] I do not get the correct Item Category Code Chair or my Item FrontHub and Desk for my Item Bicycle. I get incorrect values.
Thanks a ton in advance for any guidance.
0
Answers
-
You could use .net instead or a temp table.
https://navandbeyond.wordpress.com/2017/04/24/using-generic-list-in-net/Follow me on my blog juhl.blog0 -
I agree that it would be easy with .NET, but the problem here isn't that big or complex that it would be justified in my opinion. It's easily fixable in C/AL (or AL) without a problem by using a temp table as mentioned.
ItemCateTableRec.RESET(); ItemCateTableRec.SETRANGE(Code, "Sales Line"."Item Category Code"); IF ItemCateTableRec.FINDSET(FALSE, FALSE) THEN REPEAT TempItemCateTableRec.SETRANGE(Code, ItemCateTableRec.Code); IF NOT TempItemCateTableRec.FINDFIRST() THEN BEGIN TempItemCateTableRec := ItemCateTableRec; TempItemCateTableRec.INSERT(FALSE); END; UNTIL ItemCateTableRec.NEXT = 0;
I use .NET quite often and I know I shouldn't (e.g. creating an image file from BASE64 sent in Json to save on the drive). It's very powerful and can do way more than C/AL (or AL), but since it's not supported in BC it's best to avoid it unless you have no other choice. Otherwise it'll have to be upgraded later when you move to BC in the cloud.
0 -
It may be that you wrote your code in the wrong trigger. It's not clear under which DataItem you wrote it, but it should be under "Sales Line - OnAfterGetRecord()". If that's not it, then you might want to try out something simpler:
IF ItemCatTableRec.GET("Sales Line"."Item Category Code") THEN BEGIN //because Item Category table has only field "Code" as primary key TempItemCateTableRec.COPY(ItemCatTableRec); IF TempItemCateTableRec.INSERT(FALSE) THEN; //If it doesn't exist yet, it will get inserted END; //[..] somewhere later, after you've gone through all the categories CategoriesText = ''; TempItemCateTableRec.RESET; IF TempItemCateTableRec.FINDSET THEN BEGIN REPEAT IF CategoriesText <> '' THEN CategoriesText += ' / '; //don't need to add slash before the first category CategoriesText += TempItemCateTableRec.Code; UNTIL TempItemCateTableRec.NEXT = 0; END;
If you really want to use arrays, I'd start somewhat differently. First take all the required Sales Lines separately, sort them by Item Category Code, then loop through all of them and if the Item Category Code of the current line is different from the ICC from previous line (stored in a code type variable with default value of blank), then add it to your array. Something like:SalesLine.SETCURRENTKEY("Item Category Code"); SalesLine.SETFILTER("Item Category Code", '<>%1', ''); //not interested in blank categories and lines that are NOT Items (they won't have Item Category Code) PreviousCatCode = ''; //variable of type code[10] i := 0; //variable of type int IF SalesLine.FINDSET THEN BEGIN REPEAT IF SalesLine."Item Category Code" <> PreviousCatCode THEN BEGIN i += 1; myArray[i] := SalesLine."Item Category Code"; PreviousCatCode := SalesLine."Item Category Code"; END; UNTIL SalesLine.NEXT = 0; END;
After this, you'll have your myArray and i to show its size0 -
Hello commenters,
I found a way before I received comments here and this is how I solved but now stuck in printing the variable.
Here is what I did;
trigger OnPostDataItem()
var
sourcei: Integer;
filteredi: Integer;
intcount: Integer;
sourcelistcount: Integer;
filteredlistcount: Integer;
temp: Text[100];
begin
sourcei := 1;
filteredi := 1;
intcount := 1;
sourcelistcount := sourcelist.Count();
filteredlistcount := filteredlist.Count();
filteredlist.Insert(filteredi, sourcelist.Get(sourcei));
sourcei := sourcei + 1;
for intcount := 1 to sourcelistcount do begin
IF (sourcelist.Get(sourcei) <> filteredlist.get(filteredi)) then begin
filteredi := filteredi + 1;
filteredlist.Insert(filteredi, sourcelist.Get(sourcei));
end
else
sourcei := sourcei + 1;
end;
Clear(filteredi);
Clear(FilteredIC);
filteredi := 1;
intcount := 1;
foreach temp in filteredlist do
FilteredIC := FilteredIC + ' / ' + temp;
ItemCategoryCombined := DelStr(FilteredIC, 1, 3);
Message(ItemCategoryCombined);
end;
I get precisely what I want to print in the variable ItemCategoryCombined.
However, my Sales Quote report has 2 dataitems at this point, Sales Header and Sales Line. Adding the Column(ItemCategoryCombined; ItemCategoryCombined) in Sales Line doesn't print the value of ItemCategoryCombined in the report (rdl).
Any hint what am I missing?
Thanks in advance.0 -
Well, you're working in the "OnPostDataItem" trigger. That means all the information for that dataitem has already been processed.
Try putting your code in the "OnAfterGetRecord" trigger?
0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions