Search code examples
microsoft-dynamicsdynamics-business-central

Is it possible call a procedure using strings?


I need to call a lot of procedures with incrementing names. Is it possible to generate the name of a procedure in a string and then call it using that string? Python has its own exec method which would be ideal but I'd be fine if I could dynamically pick the name of a function.

Edit: To elaborate on what this is for, I have to write a performance test that repeats procedures of multiple codeunits. Depending on the parameters given, I could have 100 codeunits containing 30 procedures each, so to call all of them I currently have to explicitly call each and every one of them.

startTime := Time();
progressDialog.Update(1,'Proc0A');
for i := 1 to 1000 do begin
  Set0_No0.Proc0A(1);
  progressDialog.Update(2,i);
end;
timeElapsed := Time() - startTime;
if timeElapsed = 0 then begin
  timeElapsed := 1;
end;
baseTime := timeElapsed;

FinishTest(timeElapsed, baseTime, 0, false, false, 1, 1, 1000);

startTime := Time();
progressDialog.Update(1,'Proc0B');
for i := 1 to 1000 do begin
  Set0_No0.Proc0B(1);
  progressDialog.Update(2,i);
end;
timeElapsed := Time() - startTime;
if timeElapsed = 0 then begin
  timeElapsed := 1;
end;

FinishTest(timeElapsed, baseTime, 0, true, false, 1, 1, 1000);

Instead, I'd like it to work like this:

procedure CallMethod(setNo:Integer; codeunitNo:Integer; procedureNo:Integer; type:Code[5]; repetitions:Integer)
begin
  for i := 1 to repetitions do begin
    Exec('Set%1_No%2.Proc%3%4(1)',setNo,codeunitNo,procedureNo,type);
    Set0_No0.Proc0A(1);
    progressDialog.Update(2,i);
  end;
end;

Solution

  • The feature you require is not available in AL.

    There are however a couple of options you could consider:

    1. Using a combination of enums, interfaces and codeunits you can remove a lot of boilerplate code.
    2. Using a script (e.g. PowerShell) you could parse all your source files, find the codeunits and their procedures and then generate a new codeunit that invokes each procedure.