Friday, May 12, 2017

Summary of CAL CODE Programming guide

Hi,

Today I get a look on the below link for CAL Code present in NAV. I am summarising all the points from this PDF.





Note 1. There must be 1 character space between operators.

Note 2. Do not use any blank lines at the beginning and end of the function.

Note 3. Use 2 character space in alignment.

Note 4. If you want to break lines in CAL Code then use 2 character space in all.

For Eg.

MyVariable := Variable1 + Variable2 * 2 +
                     Variable3 * 3;  

Note 5. Use parenthesis alignment uniquely.

For Eg.
aaaaaaaaaa := ((xxxxxxxxxxx / yyyyyyyyyyyyy) -
                         (1 + zzzzzzzzzz / 100)) * 100;
IF (xxx <> '') AND
     ((A = 1) OR
      (B = 2)) THEN ...

Note 5. Always use // for comment with 1 space character.

Note 6. Use Value of option instead of hard Code or Integer values.

Note 7.Use Parameters to transfer Values in a Function.

... P(0); ...

PROCEDURE P(MyOption : 'Value0,Value1,Value2');
BEGIN
CASE
MyOption OF MyOption::Value0:
x := x * 10;
MyOption::Value1: x := x * 15;
MyOption::Value2: x := x * 20;
END;
END;

Note 8.  The variable you are operating on or comparing to something else must always come first in expressions.

IF (Date < a) OR (Date > b) THEN Date := c;

Note 9.  IF and THEN should normally be on the same line. ELSE should be on a separate line.

Note 10.  If the last statement in the THEN part of an IF-THEN-ELSE statement is an EXIT or an ERROR, don’t continue with an ELSE statement.

EXAMPLE
IF x <> y THEN
EXIT(TRUE);
x := x * 2;
y := y - 1;
EXAMPLE (BAD)
IF x < y THEN
EXIT(TRUE)
ELSE BEGIN
x := x * 2;
y := y - 1;
END;

Note 12.  
When BEGIN follows THEN, ELSE or DO, it should be on the same line, preceded by one space 

character.

EXAMPLE

IF (x = y) AND (a = b) THEN BEGIN
x := a;
y := b;
END;
EXAMPLE
IF (xxx = yyyyyyyyyy) AND (aaaaaaaaaa = bbb) THEN BEGIN
x := a;
x := y;
a := y;
END ELSE BEGIN
y := x;
y := a;
END;

Note 13.
Indentation of REPEAT statements:

Simple case:

REPEAT ;
     UNTIL ;

Complex case:

REPEAT ;
     UNTIL AND AND ;
REPEAT

should always be alone on a line.
IF x < y THEN BEGIN
     REPEAT
           x := x + 1;
          a := a - 1;
     UNTIL   x = y;
               b := x;
      END;

Note 14.  If there are more than two alternatives, use a CASE statement. Otherwise, use IF.

Note 15.  Within WITH-DO blocks, do not repeat the name of the object with the member variable or 
function. For example, in the following example do not replace the call of the member function 

INSERT with MyRecord.INSERT.
EXAMPLE
WITH MyRecord DO
BEGIN
INSERT;
...;
END;

Note 15. use TESTFIELD FUNCTION to check if value is not blank before assigning.

Note 16. 

To set properties from C/AL, use the following style:

"Customer No.".Visible := TRUE;
Cust.MARK := TRUE;
CurrReport.SHOWOUTPUT := TRUE;
Do not write:
Customer." No.".Visible(TRUE);
Cust.MARK(TRUE);
CurrReport.SHOWOUTPUT(TRUE);

Note 17.
Remember to set the property Editable=No on FlowFields unless you want to be able to enter values in the field.

Note 18.
As default, set the property NotBlank=Yes on the primary key fields in a table. No other fields in a table should have this property

Note 19.
Since a disabled field cannot be included in a form, never release a table with disabled fields.

Note 20.
When you apply the property ValidateTableRelation=No to a field, you should also apply the property TestTableRelation=No. Otherwise a database test on the field relations in a database may fail.

Note 21.
When programming the OnLookup trigger for a field, remember that the system will not call the code in the field’s OnValidate trigger unless you call Field.VALIDATE explicitly.
Remember also that if errors can occur in the validation, you must operate on a copy of the Rec-variable (as shown in the example below) instead of directly on Rec.

EXAMPLE

Department Code – OnLookup
WITH Cust DO BEGIN
Cust := Rec;
Dept.Code := "Department Code";
IF FORM.RUNMODAL(O,Dept) = Action::LookupOK THEN BEGIN
"Department Code" := Dept. Code;
VALIDATE("Department Code");
Rec := Cust;
END;
END;

Note 22.
As a rule, use 20 characters for a code field that is likely to be visible to external companies or organizations. Otherwise, use 10 characters.

Note 23.
To assign a value to data type DateFormula, whether it is a field or a variable, you must use the 

EVALUATE function. EXAMPLE IF FORMAT(Dateformulavariable) = ' ' THEN EVALUATE(Dateformulavariable, '1W'); 

You must use the FORMAT function to make a comparison with a text string. If you do not use this function, the IF statement will fail, because you can not compare DateFormula with data type Text.

Note 24.
To make Lookup work on a field that has a table relation to a system table, you must always explicitly set the LookupFormID property on controls showing the field

Note 25.
Remember to set the LookupFormID and DrillDownFormID properties on most tables. You cannot anticipate when a user will need to be able to activate a Lookup or DrillDown button – for example, if someone makes a report with a filter tab on the table, the Lookup button on the filter tab will not appear unless the LookupFormID property is set on the table.

Note 26.
If it is necessary to change the key before accessing a table in the database, first set the correct key, then set the correct filters, and finally, access the table. Put only the necessary key fields in a call of SETCURRENTKEY. That is, if the table order is not important, use only the fields that are used in the subsequent calls of SETRANGE and SETFILTER. This makes it possible to change the definition of the key (as long as it still includes the fields mentioned in the call of SETCURRENTKEY – in the order given) without having to change any code.

EXAMPLE
Rec.RESET;
Rec.SETCURRENTKEY(Field1,Field2);
Rec.SETRANGE(Field1,x);
Rec.SETRANGE(Field2,y);
Rec.FIND('-');


In the example, a possible key is Field 1, Field2, Field 3. Without changing the code above, the key could be changed to Field1, Field3, Field2.


No comments:

Post a Comment