Introduction: In this article I will be giving the procedure to create custom folders according to the posting date of the posted sales invoices on the click of an action.

For example: If I select three posted sales invoices say A, B and C with posting dates as 1st Feb., 2018; 6th Feb., 2018 and 6th Jan., 2017 respectively. The code checks if folders viz. ‘2018_February’ and ‘2017_January’ are present in the mentioned location or not. If not, it will create the missing folders and the Posted Sales Invoice A and B will be saved in the ‘2018_February’ and Posted Sales Invoice C will be saved in ‘2017_January’ i.e. according to their posting dates. If the folder is already present, the Posted Sales Invoice will be saved in the respective folder.

Pre-Requisite:

Microsoft Dynamics NAV

Procedure:

  1. Procedure to select multiple records on click of an action has been given in one of my previous blogs. You can refer the blog using the below link:

 https://tinamenezes.wordpress.com/2018/03/03/procedure-to-create-custom-action-to-select-multiple-records-and-modify-them-in-dynamics-nav/

In the similar way, create a custom action in Page 143 Posted Sales Invoices and add code to select multiple invoices with the help of SETSELECTIONFILTER. 

  1. Create the following global C/AL variables in Page 143-Posted Sales Invoices
Name DataType Subtype Length
FileMgt Codeunit File Management
Year Integer
MonthName Text
Folder Text 250
FileSystemAutomation Automation ‘Microsoft Scripting Runtime’.FileSystemObject
FolderName Text
DocPostingDate Date
  1. My custom folder name consists of the year followed by the month name from the posting date.

For example: 2017_March

I have created a text variable FolderName which will have the custom folder name as mentioned above. The code for the same is given below:

Year := DATE2DMY(DocPostingDate,3); //This code gets the year from the posting date
MonthName := FORMAT(DocPostingDate,0,'<Month Text>');
FolderName := FORMAT(Year)+'_'+MonthName;

I have saved the Posting date of the posted sales invoice in the variable ‘DocPostingDate’.

  1. To check if the folder of the specific month and year according to posting date in the mentioned location is present or not the following code needs to be added:
CREATE(FileSystemAutomation,TRUE,TRUE); 

Folder := // Save your folder path which can be entered in a custom setup table + ’\’ + FolderName;

IF NOT FileSystemAutomation.FolderExists(Folder) THEN        
FileSystemAutomation.CreateFolder(Folder);
  1. To save the Posted Sales Invoice document as a report in the particular mentioned path with the newly created custom folders, kindly click the below link to my next blog: 

Procedure to save a record as a report-PDF document in the particular path in Dynamics NAV

Thank you!