Invoke-NAVCodeunit with Arguments

501 views Asked by At

In the context of Powershell and dynamics NAV 2015 there is a function for running codeunits. This provides the Argument parameter. There is no documentation on how to use this parameter or how the used values ​​can be received within the codeunit.

I've tried running the codeunit with an example string, but I can't receive it anywhere in the codeunit. When running the codeunit with the MethodName parameter and arguments, I get the following error:

Invoke-NAVCodeunit : Method 'Codeunit50060.UpdateStatus(number of arguments: 1), with matching argument types.' not found.
At line:2 char:1
+ Invoke-NAVCodeunit -ServerInstance navisiontest -CompanyName d-velop  ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (0:Int32) [Invoke-NAVCodeunit], FaultException`1
    + FullyQualifiedErrorId : MicrosoftDynamicsNavServer$navisiontest,Microsoft.Dynamics.Nav.Management.Cmdlets.InvokeNavCodeunit

This is my codeunit: codeunit

1

There are 1 answers

0
Hai Nguyen On

If you call the codeunit, it will run the code in OnRun() function only. If your OnRun() calls to another method, you must set the method Local property to "No" in designer. Go to Development tool -> choose the codeunit -> go to Design -> tab Function -> select function you need to run -> click Properties buttion -> set Local to 'No' That is also helpful when you want to call the method inside codeunit directly without OnRun() defined.

then in powershell, you can call with parameters as following example:

Invoke-NAVCodeunit SampleInstance -CodeunitId 500001 -MethodName 'SampleMethod' -Argument 'SampleArg'