Wednesday, December 28, 2016

Move the pointer to the first record on NAV form

Recently I was working on a modification where I have to display an item related information in a form, the information is always changing so we need to get the new data every time they open the form. For this scenario I have created a new table and new form . We have  set the form SourceTableTemporary  property to yes , since we need to get the data and insert into the table every time they open the form.

ItemInfoINIT;
ItemInfo."Item No." := ItemNo;
ItemInfo."Line No." := LineNo;
ItemInfo.Attribute := Attribute1;
ItemInfo.INSERT;

I have added a new function to execute on the open form trigger which will retrieve the data from another SQL Server and insert the data. Everything worked fine but when i open the form. the cursor was on the last record. I want the pointer to be on the first record, when i searched on the form if there is any property that i can use to set the pointer i have not found any default property which can be set.

The solution is very simple, you just need to add the following statement in OnOpenForm triggger after inserting all the records.

IF FINDFIRST THEN;

I hope this will save time for others and especially for those who are new to the NAV development.

Please leave your comments, feedback or any suggestions you have for me to improve my blog and also if you have any questions, feel free to post.

Share:

Tuesday, December 27, 2016

How to download FTP files using .NET Interop

download-1459071_1920_thumb

Recently i have worked on a project where the requirement was to upload and download the files from a FTP.  In this blog i will be explaining how to download the files using .NET Interop. There are several other blogs which have explained, how to download the files using ScriptingHost or .net interop but i have not found an example to download all the files from a particular folder.

These are couple of blogs i found, related to this.

http://www.archerpoint.com/blog/Posts/automating-command-line-functions-nav-rtc-transmit-ftp

http://www.dynamics.is/?p=583

Below is the code that downloads all the files from a particular FTP Folder to your local download folder. The GetFTPSetup is just another function to retrieve the setup values.

I have used two functions to download the files, the first function DownloadFilesFromFTP will find all the files and add to the list, then we will loop through the list to download the file using the second function.


PROCEDURE DownloadFilesFromFTP@1240060028();
     VAR
       FTPWebRequest@1240060000 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.FtpWebRequest";
       FTPWebResponse@1240060001 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.FtpWebResponse";
       NetworkCredential@1240060002 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.NetworkCredential";
       WebRequestMethods@1240060003 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.WebRequestMethods+File";
       UTF8Encoding@1240060004 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Text.UTF8Encoding";
       ResponseStream@1240060005 : InStream;
       FileStream@1240060006 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.FileStream";
       TempBlob@1240060008 : TEMPORARY Record 99008535;
       FileName@1240060007 : Text;
       OutStream@1240060009 : OutStream;
       StreamReader@1240060010 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.StreamReader";
       List@1240060012 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Collections.Generic.List`1";
       i@1240060011 : Integer;
       PathHelper@1240060013 : DotNet "'mscorlib'.System.IO.Path";
       FileAttributes@1240060014 : DotNet "'mscorlib'.System.IO.FileAttributes";
       DotNetFile@1240060015 : DotNet "'mscorlib'.System.IO.File";
     BEGIN
       GetFTPSetup;
       FTPWebRequest := FTPWebRequest.Create(FTPSetup."FTP Download FilePath");
       FTPWebRequest.Credentials := NetworkCredential.NetworkCredential(FTPSetup."FTP UserName",FTPSetup."FTP Password");
       FTPWebRequest.UseBinary := TRUE;
       FTPWebRequest.UsePassive := TRUE;
       FTPWebRequest.KeepAlive := TRUE;
       FTPWebRequest.Method := 'NLST';
       FTPWebResponse := FTPWebRequest.GetResponse();
       StreamReader := StreamReader.StreamReader(FTPWebResponse.GetResponseStream());
       List := List.List();
       FileName := StreamReader.ReadLine();
       WHILE FileName <> '' DO BEGIN
         List.Add(FileName);
         FileName := StreamReader.ReadLine();
       END;
       FOR i:=1 TO List.Count DO BEGIN

        IF FORMAT(List.Item(i-1)) <> 'archive' THEN BEGIN
           DownloadFileFromFTP(FORMAT(List.Item(i-1)));
         END;
         
       END;
     END;

    [TryFunction]
     LOCAL PROCEDURE DownloadFileFromFTP@1240060029(FileToDownload@1240060010 : Text);
     VAR
       FTPWebRequest@1240060000 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.FtpWebRequest";
       FTPWebResponse@1240060001 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.FtpWebResponse";
       NetworkCredential@1240060002 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.NetworkCredential";
       WebRequestMethods@1240060003 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.WebRequestMethods+File";
       UTF8Encoding@1240060004 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Text.UTF8Encoding";
       ResponseStream@1240060005 : InStream;
       FileStream@1240060006 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.FileStream";
       TempBlob@1240060008 : TEMPORARY Record 99008535;
       FileName@1240060007 : Text;
       OutStream@1240060009 : OutStream;
     BEGIN
       GetFTPSetup;
       FTPWebRequest := FTPWebRequest.Create(FTPSetup."FTP Download FilePath" + FileToDownload);
       FTPWebRequest.Credentials := NetworkCredential.NetworkCredential(FTPSetup."FTP UserName",FTPSetup."FTP Password");
       FTPWebRequest.UseBinary := TRUE;
       FTPWebRequest.UsePassive := TRUE;
       FTPWebRequest.KeepAlive := TRUE;
       FTPWebRequest.Method := 'RETR';
       FTPWebResponse := FTPWebRequest.GetResponse();
       ResponseStream := FTPWebResponse.GetResponseStream();
       TempBlob.Blob.CREATEOUTSTREAM(OutStream);
       COPYSTREAM(OutStream,ResponseStream);
       FileName := FTPSetup."FTP Local Download FilePath" + FileToDownload; // 'download' + FORMAT(CURRENTDATETIME,0,'<Year4><Month,2><Day,2><Hours24><Minutes,2><Seconds,2>') +'.txt';
       TempBlob.Blob.EXPORT(FileName);
     END;

Please leave your comments, feedback or any suggestions you have for me to improve my blog and also if you have any questions, feel free to post.


Share: