Book Image

Microsoft Dynamics NAV 2009 Programming Cookbook

By : Matthew Traxinger
Book Image

Microsoft Dynamics NAV 2009 Programming Cookbook

By: Matthew Traxinger

Overview of this book

Microsoft Dynamics NAV 2009 is a business management solution that helps simplify and streamline highly specialized business processes such as finance, manufacturing, customer relationship management, supply chains, analytics, and electronic commerce for small and medium-sized enterprises. ERP systems like NAV thus become the center of a company's day-to-day operations. When you learn to program in an environment like this it opens up doors to many other exciting areas like .NET programming, SQL Server, and Web Services.Microsoft Dynamics NAV 2009 Programming Cookbook will take you through interesting topics that span a wide range of areas such as integrating the NAV system with other software applications like Microsoft Office, creating reports to present information from multiple areas of the system, and so on. You will not only learn the basics of NAV programming, but you will also be exposed to the technologies that surround the NAV system such as .NET programming, SQL Server, and Web Services.The first half of the cookbook will help programmers coming to NAV for the first time by walking them through the building blocks of writing code and creating objects like tables, forms, 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 like Microsoft Office or even custom programs. You will also discover some of the features of the Role Tailored Client including creating Pages and custom add-ins.
Table of Contents (18 chapters)
Microsoft Dynamics NAV 2009 Programming Cookbook
Credits
About the author
About the reviewer
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. Create a new codeunit from Object Designer.

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

  3. The function should take in the following parameter:

    Name

    Type

    Length

    String

    Text

    30

  4. Add the following global variable:

    Name

    Type

    I

    Integer

  5. Add the following global variables:

    Name

    Type

    Length

    OldPhoneNumber

    Text

    30

    NewPhoneNumber

    Text

    30

  6. Add 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;
    
  7. Add the following code to the OnRun trigger:

    OldPhoneNumber := '(404) 555-1234';
    NewPhoneNumber := RemoveNonNumeric(OldPhoneNumber);
    MESSAGE('Old Phone Number: %1\New Phone Number: %2', OldPhoneNumber, NewPhoneNumber);
    
  8. When you run the codeunit you will see a window similar to 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 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 numeric, we add it to our resulting string.

Note

We can only add strings to other strings so we must convert this character using the FORMAT() function. If the character is not a number, we ignore it.

There's more...

NAV comes with plenty of built-in string manipulation functions to remove characters, return substrings, find characters within string, and many more. A search in the C/SIDE Reference Guide from the NAV client help menu for string functions will give you a complete list.

Parsing strings has several uses in NAV. Some easy-to-implement examples include checking/converting a phone number to a proper format based on country code, properly capitalizing names, and removing illegal characters.

Linking records with strings

Using the Object Designer run table 6508, Value Entry Relation. You should see a column named Source RowId that contains some strange looking text. A careful examination reveals that these are not as strange as they appear. It is simply a string containing six values, each separated by a semicolon and enclosed within quotes. For example: "123";"0";"123456";""; "0";"10000".

In a typical installation involving shipments and receipts, the value of the current inventory is adjusted every time an item comes in or goes out of stock. This amount is stored in the Value Entry table. In order to know which document created which value entry, a subsidiary table was created: Value Entry Relation. In this basic scenario, the first field refers to the table that the value entry came from. The most common are: 113 for shipments and 123 for receipts. The third value stores the document number and the sixth contains the line number. Take a look at the function DecomposeRowID() in codeunit 6500, Item Tracking Management.

FOR ArrayIndex := 1 TO 6 DO
Item Tracking ManagementStrArray[ArrayIndex] := '';
Len := STRLEN(IDtext);
Pos := 1;
ArrayIndex := 1;
WHILE NOT (Pos > Len) DO BEGIN
Char := COPYSTR(IDtext,Pos,1);
IF (Char = '"') THEN BEGIN
Write := FALSE;
Count += 1;
END ELSE BEGIN
IF Count = 0 THEN
Write := TRUE
ELSE BEGIN
IF Count MOD 2 = 1 THEN BEGIN
Next := (Char = ';');
Count -= 1;
END ELSE
IF NoWriteSinceLastNext AND (Char = ';') THEN BEGIN
Count -= 2;
Next := TRUE;
END;
Count /= 2;
WHILE Count > 0 DO BEGIN
StrArray[ArrayIndex] += '"';
Count -= 1;
END;
Write := NOT Next;
END;
NoWriteSinceLastNext := Next;
END;
IF Next THEN BEGIN
ArrayIndex += 1;
Next := FALSE;
END;
IF Write THEN
StrArray[ArrayIndex] += Char;
Pos += 1;
END;

This is an amazing example of how you can manipulate strings to your advantage. The code is fairly complex and may take some time to understand, but it can give you a basis to write your own code. You should be able to see the code that looks for semicolons, or field separators, as well as the code that finds quotes, or field identifiers. The code separates out those fields and stores them in a string array for later use.

See also

  • Converting a value to a formatted string

  • Creating an array

  • Repeating code using a loop

  • Checking for conditions using an IF statement