Important Notes
The samples and information below is provided without a warranty of any kind. This post is for informational purposes and coresystems ag assumes no responsibility for errors or omissions in the information provided.
Purpose
Attached is one customize optimizer rule that will add a filewatcher to check a predefined folder and every time a new file is added to that folder the rule will fire up specified process. In this example it will write to the status bar log that a new file was found.
Requirements
The sample requires coresuite Version 3.50 or higher, and SAP Business One 8.8 or higher. It also requires the creation of an extended code functions in coresuite customize and using the library System.IO.
Procedure to use this small solution
- Download the attached file FAQ_10152_FileWatcher.cocu
- Import in SAP Business One via > Administration > Add-Ons > coresuite customize > Import / Export > Import rules. In the message box, select “All Active”. Click on “Import”.
- Create a Folder named "FileWatcher" on your C Drive
- Restart the coresuite addon
- Add files to the folder "FileWatcher"
- Look at the status bar in SAP Business One and read "New File found: <File Name>" for each file created in that folder
Procedure to adjust this small solution
To add a filewatcher for a specific folder adjust or add a new row in the coresuite customize optimizer StartUp rule "GEN_10152: Filewatcher":
CreateWatcher(@"C:\FileWatcher");
To change the behavior of the filewatcher, add your custom code in the Extended Code function watcher_FileCreated under
/* TODO: ENTER THE CODE TO EXECTUTE WHEN FILE WAS CREATED HERE */
You can also subscribe to other events like rename, delete... and create other functions for this or set a filter on which file types you want to watch.
A use case for this rule could be an automatic import process with data from an Excel File created or changed on a specific folder and much more.
Preview Sample (Optimizer Rule C#):
/* Registers a FileWatcher for the given path */
public void CreateWatcher(string path)
{
//Create a new FileSystemWatcher.
FileSystemWatcher watcher = new FileSystemWatcher();
//Subscribe to the Created event.
watcher.Created += new FileSystemEventHandler(watcher_FileCreated);
//Set the path
watcher.Path = @"" + path;
//Enable the FileSystemWatcher events.
watcher.EnableRaisingEvents = true;
}
/* this method gets called, when a new file was created */
void watcher_FileCreated(object sender, FileSystemEventArgs e)
{
//A new file has been created in the given path
string filename = get_watcher_FileCreated(this, e);
/* TODO: ENTER THE CODE TO EXECTUTE WHEN FILE WAS CREATED HERE */
StatusBar.WriteSucess("New File found: " + filename);
}
/* this method returns the filename of the newly created file */
string get_watcher_FileCreated(object sender, FileSystemEventArgs e)
{
//returns filename from new file
return e.Name;
}
Comments
0 comments
Please sign in to leave a comment.