Business Integration Solutions Saas
How To: Create a Custom Codeunit with Expression Functions that uses the Invokable0 up to Invokable10 interface
Steps
This scenario is based on a single codeunit holding a single Invoke method with a possible number of arguments going from 0 up to 10. For this you need to create your own InvokableN extension, based on how many number of arguments your own library needs. You can see below an example of an enum extension for libraries that need 3 arguments. The same should be done for the others invokables if needed.
enumextension 50000 "My Invokable 3 Enum Ext" extends TICInvokable3
{
value(50000; "My custom invokable 3 library")
{
Implementation = TICIInvokeRegistrable = "My custom invokable 3 library", TICIInvokable3 = "My custom invokable 3 library";
}
}
Your codeunit needs to implement both interfaces: TICIInvokableN and TICIInvokeRegistrable.
The TICIInvokableN interface gives you access to the Invoke method that each interface has.
!//Invokable0 Invoke
procedure Invoke(var return: Variant)
!//Invokable1 Invoke
procedure Invoke(var return: Variant; arg1: Variant)
!//Invokable2 Invoke
procedure Invoke(var return: Variant; arg1: Variant; arg2: Variant)
!//Invokable3 Invoke
procedure Invoke(var return: Variant; arg1: Variant; arg2: Variant; arg3: Variant)
!//Invokable4 Invoke
procedure Invoke(var return: Variant; arg1: Variant; arg2: Variant; arg3: Variant; arg4: Variant)
!//Invokable5 Invoke
procedure Invoke(var return: Variant; arg1: Variant; arg2: Variant; arg3: Variant; arg4: Variant; arg5: Variant)
!//Invokable6 Invoke
procedure Invoke(var return: Variant; arg1: Variant; arg2: Variant; arg3: Variant; arg4: Variant; arg5: Variant; arg6: Variant)
!//Invokable7 Invoke
procedure Invoke(var return: Variant; arg1: Variant; arg2: Variant; arg3: Variant; arg4: Variant; arg5: Variant; arg6: Variant; arg7: Variant)
!//Invokable8 Invoke
procedure Invoke(var return: Variant; arg1: Variant; arg2: Variant; arg3: Variant; arg4: Variant; arg5: Variant; arg6: Variant; arg7: Variant; arg8: Variant)
!//Invokable9 Invoke
procedure Invoke(var return: Variant; arg1: Variant; arg2: Variant; arg3: Variant; arg4: Variant; arg5: Variant; arg6: Variant; arg7: Variant; arg8: Variant; arg9: Variant)
!//Invokable10 Invoke
procedure Invoke(var return: Variant; arg1: Variant; arg2: Variant; arg3: Variant; arg4: Variant; arg5: Variant; arg6: Variant; arg7: Variant; arg8: Variant; arg9: Variant; arg10: Variant)
The interface TICIInvokeRegistrable exposes the RegisterInvokableMethods method to you, where you can register what functions you want to be available
It has the following definition
!//**********************************************************
!//<Registrar>Library that exposes ways of registering methods and arguments</Registrar>
!//**********************************************************
procedure RegisterInvokableMethods(var Registrar: Codeunit TICInvokableRegistrar);
!//**********************************************************
!//The methods that the TICInvokableRegistrar exposes can be seen below
!//<MethodName>The methods name you want to register</MethodName>
!//<MethodDescription>The methods description you want to register</MethodDescription>
!//<ReturnType>The methods return type</ReturnType>
!//<Namespace>The enum value linked to the codeunit library you are trying to register</Namespace>
!//**********************************************************
procedure RegisterMethod(MethodName: Text[245]; MethodDescription: Text[150]; ReturnType: Enum TICInvokableReturnType; Namespace: Enum TICNSInvokable)
!//**********************************************************
!//<ArgName>The argument name you want to register</ArgName>
!//<ArgType>The argument type you want to register</ArgType>
!//**********************************************************
procedure AddArguments(ArgName: Text[120]; ArgType: Enum TICInvokableParameterType)
!//**********************************************************
!//<MethodName>The argument name you want to register</MethodName>
!//<Namespace>The argument type you want to register</Namespace>
!//The var variables are there to fill by the method metadata based on the method name and namespace
!//**********************************************************
procedure GetMethodMetadata(MethodName: Text[245]; Namespace: Text; var MethodDescription: Text[150]; var ReturnType: Enum TICInvokableReturnType; var ArgumentsList: Codeunit TICInvokableMethodParamColl): Boolean
A full example of an InvokableN library can be seen below.
codeunit 50000 "My Invokable3 Library" implements TICIInvokeRegistrable, TICIInvokable3
{
procedure Invoke(var return: Variant; arg1: Variant; arg2: Variant; arg3: Variant)
var
ArgInt: Integer;
ArgDec: Decimal;
begin
ArgInt := arg1;
ArgDec := arg2;
ArgDec := Round(ArgDec);
return := Format(arg3) + Format(ArgInt / ArgDec);
end;
procedure RegisterInvokableMethods(var Registrar: Codeunit TICInvokableRegistrar)
var
ReturnType: Enum TICInvokableReturnType;
ParameterType: Enum TICInvokableParameterType;
begin
Registrar.RegisterMethod('Invoke', 'Invokable 3 method description.', ReturnType::Text);
Registrar.AddArguments('arg1', ParameterType::Integer);
Registrar.AddArguments('arg2', ParameterType::Decimal);
Registrar.AddArguments('arg3', ParameterType::Decimal);
end;
}