FOR Loop

FOR Loop or FOR Statement In Microsoft Dynamics NAV

Definition

FOR loop is a repetitive statement or loop, is used when we need to execute the code a predetermined number of times. 

FOR Loop in Microsoft Dynamics NAV, C/AL having mainly 2 types

  1. FOR .. TO Loop
  2. FOR .. DOWNTO Loop

FOR .. TO Loop

In the case of FOR .. TO Loop the value checking is incresed format and when the value reach the condition wrong then exit loop.

Syntax of FOR .. TO Loop:

--------------------------------------------------------------

FOR Control Variable :=  Start Value TO End Value

DO Statement

--------------------------------------------------------------

Here Control Variable must be a variable type of Boolean, Date, time or any numeric type. The Start value and End Value must be either expressions of same data type.

For Example:

===========================================

FOR i :=1 TO 5 DO

    Result := Result + 5;

===========================================

In this programming codes works in a manner that first intialize the value i to 1 and then check the condition that whether the i less than 5, i="i+1"

FOR .. DOWNTO Loop

In the case of FOR .. DOWNTO Loop the value checking is decresed format and when the value reach the condition wrong then exit loop.

Syntax of FOR .. DOWNTO Loop:

--------------------------------------------------------------

FOR Control Variable :=  Start Value DOWNTO End Value

DO Statement

--------------------------------------------------------------

The Control Variable, Start Value and End Value are same as  FOR .. TO Loop. Only difference is that in the case of  FOR .. TO Loop Control Variable is in the Increase format but in the case of  FOR .. DOWNTO Loop decrese format.

For Example:

===========================================

FOR i :=5 DOWNTO 1 DO

    Result := Result + 5;

===========================================

In this case programming code work in the manner like first assign the value 5 to i and then check the value 5>1 and then do the statements following. Next time i value decrese by 1 and then check the condition and soon.

Repetitive Statements or Loop

Repetitive Statements or Loop

Repetitive Statements or Loops are using when we want to execute one statement or more than one statement multiple times. In the case of IF loop it will work only for one time execution only. So if the user need to execute a statement more than one times based upon some condition. 

All the repetitive statements are works in a manner like that it will work repetedly until one condition satisfy and if that condition appear then it moves the control to next programming satement after the repetitive statements. And in the case of repetitive statements always having initialize variable and increment values will appear. 

Some of the Repetitive statements are

IF Statement, WHILE .. DO, REPEAT .. UNTIL etc.

CLEAR

CLEAR() In Microsoft Dynamics NAV C/AL

CLEAR() is used to clear the Variable. The sysntax of CLEAR()  is 

CLEAR(Variable);

CLEAR () clears the variable in the manner like if the Variable is Integer then the Variable value reset to zero. And if the variable is String then the Variable value reset to empty.

For Exapmple,

If you want to reset a Result variable that stores the sum of previous calculation then you have to mension 

CLEAR(Result);

After executing this line the calue Result goes to zero.

So the CLEAR () is most usable function in the good C/AL programing.

Syntax Of Arrays

Syntax Of Arrays In Microsoft Dynamics

Synatax

An array have to define in the following format of syntax only in the Navision (Microsoft Dynamics NAV) C/AL.

Identifier  [Index Expression1] , [Index Expression 2] , etc .

The 'Identifier' is the Variable identification of an array and this need to include any types of arrays. Only the difference occur in the 'Index Expression' area only. ie, If we need Single dimension array then we need only Index Expression1.  In that case following will be the sysntax

Identifier  [Index Expression1]

Exapmple : A[3]

In the case of Two dimensional  array we have to mension upto Index Expression2. 

And in the case of Three Dimensional Arrays we have to mension Index Expression1, Index Expression2, Index Expression3.

How to assingn values to the Arrays?

To assign values to the array element 4 as 50 then you have to mension in the following format.

A[4] := 50;

Arrays

Arrays In Microsoft Dynamics NAV

Definition:

Arrays are special type of variables that have more functionality than the simple variable or normal variables. 

An Arrays is a complex variable that actually holds group of variables and these groups is defined once with a single identifier and a single datatype.

For Example:

Identifier : QuantityArray

Data Type: Integer

Diffrent Terms of Arrays

Index

Index is used to refer to single element in an array

Element

An element is a single variable in an array.

To access the 5 element in the array of QuantityArray, you have to specify like

QuantityArray[5]

Dimension

An array can be specified in different dimensions. In the abow example QuantityArray[5] is a type of one dimensional array. Array dimension can be one or more. But one dimension arrays more simpler than multidimension arrays. For specifying the Complex datas we are using Muli dimensional arrays.

Combound Statements

Combound Statements In Microsoft Dynamics NAV

Combound Statements are statements that can be used to represent multiline statements in the IF, WHILE etc like statements.

Sysntax for combound statements are

BEGIN Statement1 {; Statement2} END

For example :

=====================================

IF Quantity <> 0 THEN

     BEGIN

              UnitPrice := ExtendedPrice / Quantity;

              TotalPrice := TotalPrice + ExtendedPrice;

     END;

=====================================

Comment

Comment In Microsoft Dynamics NAV

What is a Comment?

Comment is the useful information to identify or represent a programming code.

The comment statements are not executable so we can comment the programming codes that no need to execute also.

Types Of Comments

Mainly 3 types of comments available in the Microsoft Dynamics NAV C/AL.

  1. Single Line Comment 
  2. Block Of Comment
  3. Nested Comments
  • Single Line Comments :- Single line comments are types of comments that can used to comments single line of codes. In this case, this single line comment ignore by the compiler. The Syntax used is '//'. ie, if we are placing // in any of the program codes then after that text is consider to be comment.

For Example:

================================================

//Sample code for Comment showing adding two numbers

A := B + C;

================================================

  • Block Of Comment:- Block comments are comments that allow multiline comment. Block comments start with '{' bracket and closing with '}'. ie, the text between these brackets are considered as comment portion and this cannot be repeated.

For Example:

================================================

//Sample Block comment

{

text1........

text2......

text3

}

================================================

  •  Nested Comments:- Nested Comments are types comments that represent comments in the form, a comment containing another comment. ie, the brackets of '{' is started but inside of that also another '{' rather than '}'.

For example :

================================================

//Nested Comment

{

text1........

                    {

                       text2......

                       text4..................

                      } 

text3

}

================================================

EXIT Statement

EXIT Statement In Microsoft Dynamics NAV 

EXIT statement is a type of statements that used for skip following program  trigger for avoiding errors or other purpose. Whenever an EXIT statement was there the program quit further moving the codes of that trigger and the control backs to the objects.

For exaple consider the following codes

=============================================

IF Quantity = 0  THEN

  EXIT;

Unit Price := Total Price / Quantity;

=============================================

This statement works in a manner that once the Quantity value will be '0' then the program skip the codes preceding that for avoing the division by zero error.

IF - THEN And IF - THEN - ELSE Satement In Microsoft Dynamics NAV

IF Satement In Microsoft Dynamics NAV

IF Statement is the most commonly used conditional statement that work based upon the statement is TRUE or FALSE.

Conditional Statements works like first check one condition and based upon the result of this condition further flow of the program happened. Microsoft Dynamics NAV(previously Navision) language C/AL is used other conditional statements too.

If statement or IF loop is used execute a code when a purticular statement is TRUE only and some case its used to execute some statements when the statement is FALSE too.

There are mainly 2 types of IF statement specifications

  1. IF - THEN Syntax
  2. IF - THEN - ELSE Syntax

IF - THEN Syntax : -

In this type of IF statements you have to use following type of syntax


IF boolean expression THEN  statement


This statement works in a manner that first the Boolean expression is evaluated and if the value is TRUE then the Statement excuted and if the Boolean expression is FALSE then the statements after the IF statement is executed. ie, the program skips the IF statements.

For example consider the following example:

======================================

IF Total > 50 THEN

    MESSAGE('Total Value is grater than 50');

======================================

In this sample codes we checking that if the Total is grater than 50 or not and if the value is grater than 50 we are displaying a message that Total Value is grater than 50. 

Note that the statement is indented 2 characters, this is beacuse of the better programing practice only. We can put these codes without these space also excuting the same result also we can put these statements in immediatly preceding the THEN statements. 

IF - THEN - ELSE Syntax :-

In this type of IF statements you have to use following type of syntax


IF boolean expression THEN  statement1 ELSE statement2


This time its works in the maner that if the boolean expression is true then execute the statement1 and if the condition is false then execute statement2.

For example consider the following example:

=======================================

IF Total > 50 THEN

    MESSAGE('Total Value is grater than 50')

ELSE

   MESSAGE('Total Value is Less than or Equal to 50');

=======================================

In this sample codes we checking that if the Total is grater than 50 or not and if the value is grater than 50 we are displaying a message that Total Value is grater than 50.  And if the value is less than or equal to that then display Total Value is Less than or Equal to 50.

Form Designing And Executing In Microsoft Dynamics NAV

Form Designing And Executing In Microsoft Dynamics NAV(Navision) Using C/AL

By this topic i will tell how to design a Microsoft Dynamics NAV Form  Designing and Excetions of a simple program. 

There are mainly 5 steps we have to care when we design a Simple Form without using table in the Dynamics NAV. 

  1. Designing Form
  2. Defining Variables
  3. Assigning Variables to the designed form objects
  4. Create a programe code to excute the form
  5. Run the form
  • Designing Form:- For design a form you have to first goto the Tools -> Object Designer -> Form. Then click on the New Button. Select  create a blank form and press ok. After opening that select from the menu View -> Toll Boox,  View ->Font View -> Color.                                                                                                                                                                                                                                                                                                       Tool Box is the area where containing all the tools need to be included in the form like Label, Button, Text Box.                                                                                                                                                                                                                                                                                                                  Font is the area where we can manage all the font related activites of the text. First click on the Add Label option in the Tool Box.                                                                                                                                                                                                                                                                                                              Color is the area where we can assign colors to different objects.                                                                                                                                                                 After selecting these boxes you have to  goto the Tool Box and select the Tool Frame and place them in the form, next click Add Label, then click text box notice that both the label and text boxes created at a strech. You have to place 3 text boxes in the form and one Command Button from the Tool Box. Our aim is to add the value of two items and display the value in the thrird one named Result.
  •  Defining Variables:- For doing the abow problem we have to define 3 variables named Value1,Value2 and Result. For doing this goto the View C/AL Globals section and enter the 3 variables as Decimal Data types. ie, Value1 - Decimal, Value2 - Decimal, Result- Decimal.For defining these refer http://mibutech.blogspot.com/2010/02/sample-program-to-display-stored-data.html  link.
  • Assigning Variables to the designed form objects:- For asigning values to the object is the main area where we are connecting our designed objects to the variables we are defined. For doing this we have to select each and every text box one by one and then Goto the View -> Properties option, there look the SourceExpr and then drill down the options in the right side and select one by one variables and asign. ie, click on the first text box and then asign the Value1, select second text box and asign the Sourceexpr as Value2 and in the Third one asign Result. After doing this you will see following type screen.
  • Create a programe code to excute the form:- For calulate something using the objects input data we have to define the programming code. A sample program for calculate the addition of Value1 and Value2 and display the value in the Result is as follows.  -----------------------------------------------------------------------------------------           Result := Value1 + Value2;                                                                                                          -----------------------------------------------------------------------------------------    You have to place this in the Form OnRun(). For doing this click design buttor in the Object desginer-> Form. Goto the OnRun() and type folowing code there.     
  •   Run the form:- For run the form you have to open the Object designer then press the Run Button. Then the following picture like result you will get. 

Logical and Relational Operators & Expressions

Logical and Relational Operators In The Microsoft Dynamics NAV C/AL

Logical and Relational operators are always result in a boolean value ie, either TRUE or FALSE.

Relational Operator

Relational operator is used in the relational expressions to test a relationship between the term preceding it and the term following it, resulting in a boolean value.

These operators are listed below

  • = (equal to)
  • IN (Included in set)
  • < (less than)
  • > (grater than)
  • <= (less than or equal to)
  • <> (not equal to)

Logical Operator

Logical operator uses one or two Boolean terms in a logical expression. The logical binary operators are AND, OR and XOR (exclusive or). The one logical unary operator is NOT.

Expressions

Expressions In Microsoft Dynamics C/AL

Definition

An Expression is a formula telling the computer how to generate the desired value.Like a variable and a constant, an expression has a type and value. A expression must be evaluated at run-time in order to determine its value. Although a constant's value is known at all times, a variable value is determined at run time, the  system must look it up in memory.

Examples

Quantity * DirectUnitCost

Expressions Function Calls

Expression function call are the type of function calls that calls from the expression itself.

If you try to assign too many characters to a string variable, you will get a runtime error. How this overcomed, since the error doesnot occur until the program is running? One of the method to overcome this is to design your program such that this error could never happen. In some cases using another method. 

MAXSTRLEN Function

MAXSTRLEN function will tell you at runtime the maximum legth of a string that can fit in a variable. It has one parameter, which is the variable in question. The return value is of type integer.

Let us consider the following programing code

-------------------------------------------------------------------------------------

Description := COPYSTR('The message is: ' + CodeB,1, MAXSTRLEN(Description));

-------------------------------------------------------------------------------------

Where Description is a type text variable of length 30 and CodeB is the type of datatype 'Code' with size 50.

Once we use this type of coding style then the error will not through the system because only the Maximum string lengh only copying and displaying.

Self Practise Topic 2

Self Practise Topic 2

Varibles & Datatypes Area

  1. 1)Create an Integer Item named mynum and store the value 100 and diplay the data in a Message Box when the page run.
  2. 2) Create a program to add two decimal numbers and display the result in a Message box when the object run.

Answers:

2.1)Here i am not specifying how to define a variable, i think you already understand how to define that from the previous post. Only diff is that you have to define the variable in the Datatype column as integer. 

The program code for display that was

----------------------------------------------------------- 

mynum := 5;

MESSAGE('The Value Of %1 Is %2', 'mynum', mynum);

-----------------------------------------------------------

2.2) For doing this you have todefine 2 variables as Decimal numbers.

Let us consider Num1 and Num2 , the result storing and display from the Result variable

the programming codes are as follows

----------------------------------------------------------

Num1 := 2;

Num2 := 7;

Result := Num1 + Num2;

MESSAGE('The Value Of %1 Is %2', 'Result', Result);

----------------------------------------------------------

The result will be 9

Sample Program to display a Stored Data

Sample Program to display a Stored Data

This session describes how to define a variable as particular Data type and display their contents.

First up all we will see the assigning a text to a 'text' variable and display there contents in a message box.

  • For doing this you have to first define the variable as text variable for that goto the Tools -> Object Designer 
  • Open the object you want to do coding. (for how to open a C/AL Editor see the C/AL Editor page) 
  • After open the C/AL editor goto the View -> C/AL Globals
  • Go to the Variables tab and enter the Name as your text name let us say "sampletext"
  • Go to the Datatype column, here you can specify the datatype of the variable 
  • Got to the Length column type 30. ie, is the space where specifies the length of the text.

Variables defining area looks like follows


After defining this press Esc and goto the normal C/AL Editor window and type the following text in the OnRun()

------------------------------------------------------------------------------------

sampletext := 'My Sample Text';
MESSAGE('The Value of %1 Is %2', 'sample text',sampletext);

------------------------------------------------------------------------------------

Then you will get a output like the following when you run the program.

Variables

Definition

A Variable is a reference to the data value that can vary while user is running the application.

Variable always check actual memory location in which data is stored. Every varible has a datatype which describes the type of data that can be stored and each of them having value, this is stored actually in the memory address.

Mainly 3 types of variables are there

  1. Global Variables
  2. Local Variables
  3. System Defined Variables

Global Variables:-If a variable can be accesse anywhere in an object, its said to have 'Global' scope.

Local Variables:-If the accessibility is limitted to a single trigger in an object, that type of variable is called 'Local' variable.

System Defined Variables:- A variable that is maintained automatically by the system. Examples- Rec in Table object, CurrReport in Report object

MESSAGE Function

MESSAGE Function

MESSAGE Function is simplest function in the Microsoft Dynamics NAV. By the help of these function we can able to display a message box at the time of runing an Object. This is commonly used to display Warning Message, testing message etc.

First up all i will tell how to display a "HELLO WORLD" text in the Message Box. For that first we have open a C/AL editor,

for how to open C/AL editor click on the Mibutech: C/AL Editor.

Then you have to type the following code in the C/AL editor

MESSAGE('HELLO WORLD');

After entering this you have to save the code and Run the code. Then you will get a window like the following picture.

Excercises

To display your name in the message box type the following code

MESSAGE('THE VALUE OF %1 IS %2','Myname is','Jubel Thomas Joy');

If you type the message like this then you will get the display like 


Data Types

Definition:

Data Types are diffrent kinds of information that may appear in the C/SIDE Environment.

Different data types having different values and each of them having diffrent way of representation. 

Example:

If we have two data types "12" and "36" and add them, we will get different values according to different data types. ie, If they are numbers then the result would be 48 and on the other hand if we consider these are text then the result would be "1236" only.

Data Types can be mainly divided in to 4 types

  1. Simple Datatypes
  2. Numeric Data Types
  3. String Data Types
  4. Boolean, Date and Time
  • Simple Datatypes: Simple data types are those types of data which have only one value and which cannot be broken up into other values of different types.                                                    
  • Numeric Data ypes: Numeric Data Types are all forms of numbers or amounts.

TYPERANGESIZEEXAMPLEDEFAULT VALUE
IntegerFrom 2,147,483,647 to + 2,147,483,6474 Bytes

12

1000

-100

0

0
DecimalFrom -1X10^63(1 followed by 63 zeros) to + 2,147,483,64712 Bytes

12.50

-2.0

0.008

0
Option

Red, Orange, yelow, Green, Blue, Indigo, Violet Where 0 is Red and 3 is Green

0
Char0-2551 Bytes

'b'

'C'

'3'

'?'


  • String Data Types: 

TYPERANGESIZEEXAMPLE
TextFrom 0 to + 250add one to the length and round up to the nearest four. Thus and 8 char text takes up to 12 bytes (1+8 round up to the nearest 4)

"Hello"

'127.50'

Code

All Letters are forced

to upper case and all leading and 

trailing spaces are removed.

add 2 to the length and round

up to the nearest 4. Thus an 11 character code 

takes up 16 bytes (2+11 round up to the nearesr 4)

'HELLO'

'127.50'

  • Boolean, Date and Time
TYPERANGESIZEEXAMPLE
Boolean

1 Byte in memory only 

TRUE

FALSE

Date

1/1/0000 through 12/31/9999

4 Bytes

0D (The undefined date, less than all other dates)

123197D (Dec 31 1997)

Time

00:00:00through23:59:59:999

4 Bytes

1030000T (10:30am)

0T(The undefined time, less than all other times)

C/AL Editor

How C/AL Editor Look Like?

The C/AL Editor looks like following picture. Each C/AL editors are different divisions. These details are explained as follows.

Each gray bar is called as Triggers and C/AL code that you may see listed below the gray bar is the "Trigger Code" for that trigger. Not that some triggers not having any codes thse are called as empty triggers.

In the C/AL Editor mainly 3 types of Triggers available

  1. Documentation Trigger
  2. Event Trigger
  3. Function Trigger
  • Documentation Trigger

Documentation triggers is not really a trigger, it will not run any codes. Normally this place is used for the documantation purpose only. ie, to specify the modifications to the standard objects.

  • Event Trigger

Event Trigger is trigger used to handle the triggers in the system. All the Event triggers are start with "On". OnRun event trigger is executed whenever that is to be Run.

  • Function Trigger

These triggers are executed whenever you call a function in an object. 

Self Practise

Self Practise Topic 1 

For opening the C/AL codes in different areas

  1. 1) How to open the C/AL code of Form No. 26 (Vendor Card)?
  2. 2) How to open the C/AL Code of Report No.3 (G/L Register)?
  3. 3) How to open the C/AL Code of XMLport 8002 (Item)?

Answer:

1.1) To do this you have to first open the object designer from the Tools->Object Designer or Press Shift+F12.

Then click on the Form option then select the form no 26.

Then click on the Design button.

Then press F9 to view the codes of the Vendor card.

1.2) To do this you have to first open the object designer from the Tools->Object Designer or Press Shift+F12.

Then click on the Report option then select the Report no 3.

Then click on the Design button.

Then press F9 to view the codes of the G/L Register.

1.3) To do this you have to first open the object designer from the Tools->Object Designer or Press Shift+F12.

Then click on the XMLport option then select the XMLport 8002.

Then click on the Design button.

Then press F9 to view the codes of the Item.

How to view the C/AL Codes?

How to view the C/AL Codes?

You can view the view the C/AL codes from the Object Designer option in the Microsoft Dynamics NAV.

To open Object Designer you have to follow below steps:

  • Go to the menu part of the Navision client.
  • Then go to the Tools -> Object Designer other wise press the short cut key Shift + F12 from your key board.
  • There you can view following elements Table, Form, Page, Report, Dataport, XML Port, Code Unit etc. All these having C/AL codes expect Page. First up all we will see how to view C/AL for a code unit.
  • To open a C/AL code for a Code Unit you have to click Code Unit Button then select the code unit number 5804 - ItemCostManagement. Then press OK
  • Click Design Button in the lower side to view the code.

What is C/AL?

What is C/AL?

C/AL(Client Application Language) is the programming language using in the C/SIDE(Client/Server Integrated Developemnt Environment) in the Microsoft Dynamics NAV previously Navision.

By the help of this language we have following type of uses in Microsoft Dynamics NAV environment.

  • Form objects and Report objects are handling the data presentation
  • Data storage and organization is handled with help of table objects and built in Database Management System(DBMS)
  • Data acquisition is handled with the help of Form and Dataport Objects

Welcome Message

Welcome all to our new blog that intended to discuss about Microsoft Dynamics NAV Technical part. Our great intention to start this blog was the great support given by the users for Navision Planet - http://microsoft-dynamics-nav.blogspot.com

We aim to give all the persons who like to learn about the Technical part of the Microsoft Dynamics NAV- Previously Microsoft Navision quality support. In this blog we will explain each and every technical part of the Navision with simple sentenses to learn an average person. Also we think provide you an Online Support Facility also.

The name MIBUTECH comming from the Microsoft Bussiness Solutions Technical only. Hope all of them will accept this blog also with great success.

Regards

Jubel Thomas Joy

Related Posts with Thumbnails