Business central blog
Subscribe to blog and get news about new posts.

Should we use IsEmpty() for ModifyAll() and DeleteAll()?

I've noticed that BC/navision developers often ask if we need to check for emptiness before using the ModifyAll() and DeleteAll() methods? After all, everything works without checking. Well, let's find out

General

In order to understand this question, we will need to see what SQL queries AL generates during the execution of IsEmpty() + DeleteAll()/ModifyAll() and without IsEmpty(), and we will consider scenarios when the table has data on filters and when they are not.
It's actually a pretty standard function in other programming languages to get the current call stack. It is rather strange that the Business Central platform still does not have this feature. So what should we do if we need to know the current CallStack right now?
There is one way. It is codeunit 28 "Error Message Management", it contains GetCurrCallStack function, let's test its functionality.

var
  ErrorMessageMgt: Codeunit "Error Message Management";
  Customer: Record Customer;

Customer.FindFirst();
Customer.Name := 'This change will be commited!';
Customer.Modify(true);

Message(ErrorMessageMgt.GetCurrCallStack());

Message('Code is not blocked!');
Works wonders, is it some kind of magic and hidden system function? No, it's actually more simple than that, let's look at the implementation.

procedure GetCurrCallStack() CallStack: Text;
var
	Len: Integer;
	Pos: Integer;
	SubString: Text;
begin
	if ThrowError() then;
	CallStack := GetLastErrorCallStack();
	SubString := '"Error Message Management"(CodeUnit 28)';
	Len := StrLen(SubString);
	repeat
		Pos := StrPos(CallStack, SubString);
		CallStack := CopyStr(CallStack, Pos + Len);
	until Pos = 0;
	CallStack := CopyStr(CallStack, StrPos(CallStack, '\') + 1);
end;

[TryFunction]
local procedure ThrowError()
begin
	// Throw an error to get the call stack by GetLastErrorCallstack
	Error('');
end;
That is, we call an empty error in TryFunction to get GetLastErrorCallStack and remove the reference to the part of the CallStack where the empty error occurred.
While we don't have a method in AL Language yet, we can use this method from "Error Message Management".
March 7, 2024