Dynamics NAV Helpers #1 - Input Dialog

It does not matter how you stay to classic dialog forms provided by the system, but one thing is fact: Input dialogs are handy. Thank god C/AL provides a native function to use input boxes.

NewControlID := Dialog.INPUT([ControlID][,Variable])  

Luckily, someone had the brilliant idea to suspend the support for native input dialogs in Dynamics NAV 2009 RTC and replace it by: nothing.

How would someone need such a rudimental feature that was there since the beginning of time anyway? Well, it turns out, I do. Quite a lot actually. And no: wasting expensive pages and objects for simple input requests is not an option. Someday I came across a handy little thing which I am still using today, sitting deeply in my utilities Codeunit. Little snippets and helper functions I am collecting to work around lacking, unnecessary features like StringReplace or IsNumeric. And there is an alternative for INPUT as well. SimpleInput.

What you need

Create a new function, for example SimpleInput with the following parameters

question     : Text(30)  
title        : Text(30)  
defaultValue : Text(30)

Return:  
inputResult  : Text(250)  

Don't make the size of the text variables too long, since you need to concat them later - and we all know what happens when we go beyond a certain length of text in Dynamics NAV...

Now you will need to add the Windows Scripting Host COM to your local variable scope together with a temp text variable and a variable for the carriage character (CR). Now the magic begins.

Invoking the Scripting Host

// in case we are not on client side
IF NOT GUIALLOWED THEN  
  EXIT('');

// create com object
IF CREATE(objWSH, TRUE, TRUE) THEN BEGIN  
  // carriage char 
  CR[1] := 13;
  CR[2] := 10;

  // set the language, you can use VBScript and JScript (not JavaScript)
  objWSH.Language := 'VBScript';
  objWSH.AddCode(
    'function Input(question, title, default_value)' + CR +
    '    Input = InputBox(question, title, default_value)' + CR +
    'end function'  
  );

  // execute the Input function we declared at AddCode
  tmpString := COPYSTR(FORMAT(objWSH.Eval(
    'Input("' + question + '", "' + title + '", "' + defaultValue + '")'
  )), 1, MAXSTRLEN(tmpString));

  // in case the user pressed cancel or empty string, use the default
  IF tmpString = '' THEN
    tmpString := defaultValue;

  EXIT(tmpString);
END;

// in case the com object could not init
EXIT(defaultValue);  

An now use it

The above function seems very self-explaining. And so is the usage.

txtResult := SimpleInput(  
  'How do you rate this function?',
  'A quick survey',
  ''
);

MESSAGE(txtResult);  

Or, let's assume you need to ask the user for a item number while he is calling an Action.

// CUHelper is the codeunit where I store my helpers
itemNo := CUHelper.SimpleInput(  
  'What is your favorite item no.?',
  'Enter an item no.',
  ''
);

// do some cleanup, like sanitize user input/data
// ...
// end of cleanup

// lookup item
IF recItem.GET(itemNo) THEN  
  MESSAGE('Your favorite item is #%1 - %2', 
    recItem."No.", 
    recItem."Description"
  );

That's basically it. I hope someone finds it handy. At least I did as I found this (kind) of solution on some other blog.

Ellogwen (Daniele)

Read more posts by this author.

Germany