Hi, Readers.
Today I would like to briefly discuss an interesting question I saw in Business Central Forum before, can we show the string count (number of strings) of a field in Business Central, for example, 18/100.
This is not a difficult question. We can easily show this using the following two methods.
Text.StrLen(Text) Method: Gets the length of a string you define.
Text.MaxStrLen(Text) Method: Gets the maximum defined length of a string variable.
For example,
Very simple, we can also create a field to display it, but is it possible to display it after the standard caption? For example, the location in the screenshot below.
Yes, it’s possible, but it takes a few tricks.
Test video:
How is this done? We have discussed how to change field captions dynamically in Dynamics 365 Business Central before, this is an example of how this feature can be used.
We can use the CaptionClass Property.
CaptionClass Property: Controls the caption that is used in the label of a field in a database table or in the label of a control on a page. For example:
CaptionClass = ‘1,5,,’ + WordCount;
In the standard, the most commonly used fields are Dimension related fields.
Because it is not clearly explained in Microsoft’s documentation, the following information is extracted from the forum.
The first parameter says it is a dimension
https://forum.mibuso.com/discussion/6746/customized-captions-at-run-time-without-changing-codeunit-1
The second parameter says to use a certain format (= optional parameter 1 + dimension caption + optional parameter 2) of the dimension-code (not value-code!) put in parameter 3.
The third parameter: The dimension-code you want. This parameter must contain a value that does not exist in the dimensions (a blank for example). If this dimension does not exists, codeunit 1 shows the fourth parameter on the caption.
The fourth parameter: The value you want to show as caption.
So we can do it with the following code.
Very simple, give it a try!!!😁
Source code: Github (Please note that the source code is for reference only, you can improve it according to your own needs)
pageextension 50223 CustomerCardExt extends "Customer Card"
{
layout
{
modify(Name)
{
CaptionClass = '1,5,,' + WordCount;
trigger OnAfterValidate()
begin
GetWordCount();
end;
}
}
var
WordCount: Text;
trigger OnAfterGetRecord()
begin
GetWordCount();
end;
local procedure GetWordCount()
var
Length: Integer;
MaxLength: Integer;
begin
Length := StrLen(Rec.Name);
MaxLength := MaxStrLen(Rec.Name);
WordCount := Rec.FieldCaption(Name) + ' (' + Format(Length) + '/' + Format(MaxLength) + ')';
end;
}
END
Hope this will help.
Thanks for reading.
ZHU
コメント