Book Image

Microsoft Dynamics NAV 7 Programming Cookbook - Second Edition

Book Image

Microsoft Dynamics NAV 7 Programming Cookbook - Second Edition

Overview of this book

Microsoft Dynamics NAV 7 is a business management solution that helps simplify and streamline highly specialized business processes. Learning NAV programing in NAV 7 gives you the full inside view of an ERP system. Microsoft Dynamics NAV 7 Programming Cookbook covers topics that span a wide range of areas such as integrating the NAV system with other software applications including Microsoft Office, and creating reports to present information from multiple areas of the system,. We will not only learn the essentials of NAV programming, you will also be exposed to the technologies that surround NAV including.NET programming, SQL Server and NAV system administration. Microsoft Dynamics NAV 7 Programming Cookbook is written in a direct, to-the-point style to help you get what you need and continue working in NAV. The first half of the cookbook will help programmers using NAV for the first time, by walking them through the building blocks of writing code and creating objects such as tables, pages, and reports. The second half focuses on using the technologies surrounding NAV to build better solutions. You will learn how to write .NET code that works with the NAV system and how to integrate the system with other software applications such as Microsoft Office or even custom programs. You will learn everything you need to know for developing all types of NAV CSIDE objects, as well as how to integrate and maintain a NAV system.
Table of Contents (20 chapters)
Microsoft Dynamics NAV 7 Programming Cookbook
Credits
About the Author
About the Reviewers
Acknowledgements
www.PacktPub.com
Preface
Index

Manipulating string contents


It can be very useful to parse a string and retrieve certain values. This recipe will show you how to examine the contents of a string and manipulate that data.

How to do it...

  1. Let's create a new codeunit from Object Designer.

  2. Add a function called RemoveNonNumeric. It should return a text variable called NewString.

  3. Add the following parameters for the same function:

    Name

    Type

    Length

    String

    Text

    30

  4. Now add the following global variables:

    Name

    Type

    Length

    OldPhoneNumber

    Text

    30

    NewPhoneNumber

    Text

    30

    I

    Integer

     
  5. Write the following code to the RemoveNonNumeric function:

    FOR i := 1 TO STRLEN(String) DO BEGIN
    IF String[i] IN ['0', '1', '2', '3', '4', '5', '6', '7','8','9'] THEN
      NewString := NewString + FORMAT(String[i]);
    END;
  6. Write the following code into the OnRun trigger of the codeunit:

    OldPhoneNumber := '(230) 299-876';
    NewPhoneNumber := RemoveNonNumeric(OldPhoneNumber);
    MESSAGE('Old Phone Number: %1\New Phone Number: %2', OldPhoneNumber, NewPhoneNumber);
  7. To complete the task, save and close the codeunit.

  8. On executing the codeunit, you should see a window similar to the one shown in the following screenshot:

How it works...

A string is actually an array of characters. The same array syntax will be used to access the individual characters of the string.

We start with a FOR loop that begins at the first character, with index 1, and goes on until we reach the end of our string. This is determined using the STRLEN() function, which stands for string length. As the first index is 1, the last index will be N or the number of characters in the string.

Next, we access the character at that index using square brackets. If the character is a number, meaning we want to keep it because it is a numeric value, we add it to our resulting string.

Note

NAV comes with plenty of built-in string manipulation functions to remove characters, return substrings, find characters within strings, and many more. A search in the Developer and IT Pro Help option of the Help menu of Microsoft NAV Development Environment for string functions will give you a complete list.

There's more...

The CheckIBAN function of the 79, Company Information table is a simple example of string manipulation to validate IBAN (International Bank Account Number). IBAN is internationally agreed on and adopted. It consists of up to 34 alphanumeric characters: the first two letters are the country code, then two check digits, and finally a country-specific Basic Bank Account Number. The same is validated by manipulating the input string using various functions. The following code gives you an example for the same:

IF IBANCode = '' THEN
  EXIT;
IBANCode := DELCHR(IBANCode);
Modulus97 := 97;
IF (STRLEN(IBANCode) <= 5) OR (STRLEN(IBANCode) > 34) THEN
  IBANError;
ConvertIBAN(IBANCode);
WHILE STRLEN(IBANCode) > 6 DO
  IBANCode := CalcModulus(COPYSTR(IBANCode,1,6),Modulus97) + COPYSTR(IBANCode,7);
EVALUATE(I,IBANCode);
IF (I MOD Modulus97) <> 1 THEN
  IBANError;

There are a few more functions used to validate the string; such as ConvertIBAN, CalcModulus, and ConvertLetter. These functions can give you a basic idea to write your own code.

For more complex examples, please follow the DecomposeRowID() function in the 6500, Item Tracking Management codeunit. The code evaluates the value stored in the Source RowId field of the 6508, Value Entry Relation table.

See also

  • Converting a value to a formatted string

  • Creating an array

  • The Repeating code using a loop recipe in Chapter 2, General Development

  • The Checking for conditions using an IF statement recipe in Chapter 2, General Development