How to do it...

  1. Open your AL project in Visual Studio Code.
  2. In Explorer, create a new file named My Test Library.al. In the Editor, create a new codeunit object:
codeunit 50151 "My Test Library"
{

}
  1. Add the following function to the codeunit:
procedure CreateTelevisionShow(var TelevisionShow: Record "Television Show")
var
LibraryUtility: Codeunit "Library - Utility";
begin
CLEAR(TelevisionShow);
TelevisionShow.Validate(Code, LibraryUtility
.GenerateRandomCode20(TelevisionShow.FieldNo(Code),
Database::"Television Show"));
TelevisionShow.VALIDATE(Name, TelevisionShow.Code);
TelevisionShow.MODIFY(TRUE);
end;
  1. Add the following function to the codeunit:
procedure CreateTelevisionShowCode(): Code[20]
var
TelevisionShow: Record "Television Show";
begin
CreateTelevisionShow(TelevisionShow);
exit(TelevisionShow.Code);
end;
  1. And finally, add the following function to the codeunit:
procedure DeleteTelevisionShow(TelevisionShowCode: Code[20])
var
TelevisionShow: Record "Television Show";
begin
TelevisionShow.Get(TelevisionShowCode);
TelevisionShow.Delete(true);
end;

You can also see that we created two different functions to create a new record. One requires you to pass in an actual TelevisionShow: Record variable, but with the other one, you can simply call it and use the return value to identify the new record that was created. Creating standards like this will provide you with more flexibility in writing your tests.