How simply to do "try-except" in NAV

Yaroslav_GaponovYaroslav_Gaponov Member Posts: 158
edited 2010-09-28 in NAV Tips & Tricks
Hi
I propose simple method for catch an errors in NAV.

Need such Automation:
VBScript Automation 'Microsoft Script Control 1.0'.ScriptControl

Example:


IF NOT ISCLEAR(VBScript) THEN
CLEAR(VBScript);
CREATE(VBScript);

VBScript.Language := 'VBScript';

Source := STRSUBSTNO('' +
'Function TryExceptMethod' + '%1%1' +
' On Error Resume Next' + '%1%1' +
' TryExceptMethod = FALSE' + '%1%1' +
' a = 10 %2 0' + '%1%1' + // except!!!
' IF Err.Number = 0 THEN' + '%1%1' +
' TryExceptMethod = TRUE' + '%1%1' +
' END IF' + '%1%1' +
'End Function','#','\')
;

WHILE STRPOS(Source,'##') > 0 DO BEGIN
Source[STRPOS(Source,'#')] := 13;
Source[STRPOS(Source,'#')] := 10;
END;

VBScript.AllowUI := FALSE;
VBScript.AddCode(Source);
MESSAGE('Result = %1',VBScript.Eval('TryExceptMethod'));

CLEAR(VBScript);

The kernel of the problem:
Use a VBScript (JScript) Automation and command "On Error Resume Next".

p.s. you can pass a variables from NAV to VBScript like Automation etc... so VBScript.AddObject('objConnection',ADOConnection);
and execute all methods it from script.

Goog luck!!!

Comments

  • ajhvdbajhvdb Member Posts: 672
    This way you circumvent the error in NAV?
  • Yaroslav_GaponovYaroslav_Gaponov Member Posts: 158
    Hi

    I use like solution for catch an errors for work with ADO Automation when I need to get a list databases on server. If some database in suspend status than user show an errors.

    So if you need catch an errors for some Automation you can pass it as variable to script and execute the problem method.
  • kirankumarkirankumar Member Posts: 29
    Hi

    I have Compiled the code U have provided.
    Fine But I did't able to understand How the 'Add Object' thing works.
    I have added for ADO connection the Automation Variable 'Microsoft Active X Control Object Library 2.7'.

    Can you Plz Explain How we able to track errors with this code.

    Regards
    Kiran.
    Hi
  • Yaroslav_GaponovYaroslav_Gaponov Member Posts: 158
    Hi

    I think all clear in this example.

    CREATE(objConnection);
    objConnection.[SOME_PARAMETERS] := [SOME_VALUES]
    ...
    VBScript.AddObject('objConnectionXXX',objConnection);
    ...
    Source := STRSUBSTNO('' +
    'Function Test' + '%1%1' +
    ' Test = FALSE' + '%1%1' +
    ' On Error Resume Next' + '%1%1' +
    ' objConnectionXXX.[SOME_METHOD_FOR_EXAMPLE]' + '%1%1' +
    ' IF Err.Number = 0 THEN' + '%1%1' +
    ' Test = TRUE' + '%1%1' +
    ' END IF' + '%1%1' +
    'End Function','#')
    ;

    ...
  • PureHeartPureHeart Member Posts: 190
    =D> =D> =D> =D>
    I just want to say you are a very clever guy Yaroslav!
    Thank you for sharing!
    Why don't you try my compare tool?
    http://www.mibuso.com/dlinfo.asp?FileID=1123
Sign In or Register to comment.