Friday, August 5, 2016

Using With Statement in C/AL code | Make Code Simple Series

How to write lesser code in NAV...!!??!!

There is always a better way. I am fortifying my Programming skills everyday with some customized code. I like to make coding life interesting so i keep on making small changes to make life easier and programming more intelligent. 

I would like to share today about With Statement. When we work with records(except Current record 'rec'), addressing is created as record name, dot (period), and field name:

For example let say we are coding into Sales line table with 'Item' record. then we need to continuously use "Item.FieldName" and if we need to write code for ten or twenty fields or any field repeatedly then it makes no sense using Item(Dot) always. This is where we should know WITH STATEMENT. 

WITH <Record> DO
  "No." := '1234';         //Instead of Item."No."
  Description:= 'Pokemon GO Server';      //Instead of Item.Description
  "Unit Cost" := '50000.50';       //Item."Unit Cost"
//Other codes


Thursday, August 4, 2016

Installing multiple Instances/DB | Dynamics NAV | on a single Machine | For Beginners

Hello Folks,

I recently felt many of my clients(technical team) are facing problem understanding the process involved in creating multiple instances of databases. Generally in every server we have a TEST DB and a LIVE DB(there can be more depending on the nature and stage of project). Inside every DB there can be as many Company as the system can hold.

For technical personnel, the main concern is how to maintain two databases, one for testing Objects sent from the Developers and then after successful testing can be transferred to the Live DB for final level testing... Generally all the objects are kept similar between Test and Live DB(may not be the case always).

You can always find these technical details on in details but in most of the cases i have seen, users feel uncomfortable reading all those walk-through and most of them prefer either video tutorial or the blogs with images.

I am assuming Version 2016 since i'll be attaching snaps of version 2016.
Let us assume we already have LIVE DB. It has Instance Named "DynamicsNAV90", which is the default name...
The ports used by DynamicsNAV90 are:
Client Services: 7056
SOAP Services: 7057
OData Services: 7058
Management Services: 7055 

Now lets replicate the Database. We can do this from SQL Server Management Studio.
I'll not go in details since this is all normal steps...

Now lets assume we have a New Database named "Demo Database NAV (9-0)_new"...

Now lets create a New Instance from "Dynamics NAV 2016 Administration", Lets right click on Console Root/Microsoft Dynamics NAV(Local) and click on Add Instance.

It will populate a new window where we are supposed to enter the server name and different ports. Lets use different port since we don't want port conflict issue and this is the main part, since we are assigning totally different port to a new instance they will be totally independent entity and they can run independently. Lets now assign the Database we just created "Demo Database NAV (9-0)_new" to this instance(ForTestDB is our new Instance).

New Instance Name: ForTestDB
Client Services: 5060
SOAP Services: 5061
OData Services: 5062
Management Services: 5059
(you can choose any available Ports, just keep the port number together and in the same range, it helps you remember the ports)

Now lets go to Services(go to RUN(win +R) and enter Services.msc and press enter) and confirm that both the services are running(see the snapshot).

Now we are all configured. We just need to go to Development environment and check the Database information and confirm the Server Instance has a value in General section.

Now lets run RTC. First Client will run automatically since it was working before. Now let open RTC again(keep previous RTC open) in the left side we have a Down arrow icon click on it and select "Select Server". Here we will have to enter the Server Address manually. the format of Server address is :"localhost:"ClientServiceport"/InstanceName" so in my case it is "localhost"5060/ForTestDB".

Here you go, you have two NAV RTC running on the same machine. You can follow the same step to install and run as many NAV system on your machine as your system can. This also works for other versions and inter-version installation...


Monday, August 1, 2016

Export to CSV File | RTC/CLASSIC Report

Statement: Save the content of Report to CSV file in a Specific Template.
One of our client came of with a requirement to generate a 'Comma Separated Value' Format Data from a report (Bank Account - Check Details).

There are many ways to do this,  i used File Method...

For the sake of this example i am using classic Client. It works sames in RTC as well(up to NAV- 2016 Tested).

Global Variables Created

tpath  [Text-100]
tFilename 9 [Text-50]
lfi_ExportFile [File ]
ltx_Line [Text-1024]
ltx_CRLF [Text-30]
ltx_LineHead [Text-250]
lBankAccount [Integer]
lCheckLedgerEntry [Record-Check Ledger Entry]
SalesReceivablesSetup [Record-Sales & Receivables Setup]
CreateCSVfile [Boolean]
Total_Amount [Decimal]
Total_AmountVoided [Decimal]
Total_AmountPrinted [Decimal]


Bank Account - OnPreDataItem()
CurrReport.NEWPAGEPERRECORD := PrintOnlyOnePerPage;
  RecordCounter :=0;


//For New Line...
  ltx_CRLF := '';
  ltx_CRLF[1] := 13;
  ltx_CRLF[2] := 10;

//For Textmode ON

//For the Location of CSV file
    IF  SalesReceivablesSetup."Check Detail- CSV File Locatio"<>'' THEN
    tpath := SalesReceivablesSetup."Check Detail- CSV File Locatio"
     ERROR(Error Message);

 //File Name Format
    tFilename := FORMAT(tpath) + 'Check Detail-'+ FORMAT(TODAY,0,'<day,2>-<month,2>-<year,2>') +
     FORMAT(TIME, 0, '<hour,2><minute,2><second,2>') +'.' + 'csv';
    IF CreateCSVfile= TRUE THEN


Integer - OnAfterGetRecord()
IF NOT CheckLedgEntryExists THEN

//For the given format...
 ltx_Line :=  "Bank Account"."No."+ ','+'' +','+''+ ','+ '' +','  +    DELCHR(FORMAT(Total_Amount),'=',',') +','
   + DELCHR(FORMAT(Total_AmountPrinted),'=',',') +','  +DELCHR(FORMAT(Total_AmountVoided),'=',',') ;

//Every line will be copied to ltx_Line & get copied to CSV file instead of storing multiple line.

//Resetting the Total Values
Total_Amount :=0;
Total_AmountVoided := 0;
Total_AmountPrinted :=0;

Report - OnPostReport()
   IF CreateCSVfile= TRUE THEN

//Code in Report Section

Bank Account, Body (7) - OnPostSection()
IF CreateCSVfile= TRUE THEN BEGIN //Boolean in Request Page...
IF CheckLedger("Bank Account") THEN
lfi_ExportFile.WRITE("Bank Account".Name);

Check Ledger Entry, Body (2) - OnPostSection()
ltx_Line := ''+','+ FORMAT("Check Date") +','+ "Check No." + ','+
 DELCHR(Description,'=',',') + ','  + DELCHR(FORMAT(Amount),'=',',') +','
   +DELCHR(FORMAT(AmountPrinted),'=',',') +','  + DELCHR(FORMAT(AmountVoided),'=',',') +','
    + FORMAT("Entry Status") +','  +FORMAT("Original Entry Status")+
     ','  + FORMAT("Bal. Account Type") +','  +FORMAT("Bal. Account No.");


//Calculation of Total...
Total_Amount +=Amount;
Total_AmountVoided  += AmountVoided;
Total_AmountPrinted += AmountPrinted;



Note: If you want to try the same in RTC report then Use the code of Section in Dataset... It works absolutely fine in RTC as well... I tested it in 2013, 2015 and 2016 as well...


Print Preview - Content not showing | RTC Report

A Newly created report which is working fine is not showing the Line Section only for a couple of documents.
But when the same report is saved as PDF or any other format available, it is working fine.
All the codes and #dataset is correct.

For confirmation i even checked the records of the report...

It is pulling all the records and is normal. 
I rechecked all the codes but couldn't find the mistake. Then finally i started talking to some of my colleagues. One of my friend just presented a random solution and surprisingly it worked. This is the reason i am writing this blog. I cannot explain why it happens but it works...

The problem was big but the solution was just a report property option. The preview mode of the report is "Normal". In order to make this report get the Line value and preview it i changed the Preview mode to "PrintLayout" and it worked fine...

It doesn't makes enough sense why the same report was working fine for other documents and not for some of the documents(all the Documents are of almost similar line records).  

Hopefully someone will find this Blog helpful. I would be happy if i can save someone time with this solution. 

Thank you.