Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX forum
Answered

Unable to get filename while running class in batch mode.

Posted on by

Hi Experts,

I have a class extending from runbase (cannot replace with sysoperation due to internal reasons) .

Facing 2 issues with this class, please suggest a fix 

1. How can i ensure that when use browse to select a file it only shows *.CSV files in the browse window. LINE 23 in below code

2. Code runs without issue when running in non batch mode with Run on property set to "Called from or Client" , however

when running in batch it keep throwing the error "No file selected " even when a file is selected.   LINE97 in below code for same run on properties.

We cannot change the class run on to server as the end users will be browsing & selecting a file from their local system & not server or network share.

Shared below is the code & I have followed this pack /unpack using this MSFT link 

class myClass extends RunBaseBatch
{
    CommaIo                     csvFile;
    Filename                    filename;
    DialogField                 dialogFilename;
    

    #define.CurrentVersion(2)
    #define.Version1(1)

    #localmacro.CurrentList
        filename
    #endmacro
    #File

protected Object dialog()
{
    DialogRunbase       dialog = super();
    ;
  
    dialogFilename  = dialog.addField(extendedTypeStr(FilenameOpen));
    dialogFilename.value(filename);
    dialog.filenameLookupFilter(["All files", #Allfiles] #csv]); // HOW TO ENSURE ONLY CSV SHOWS UP

    return dialog;
}

public boolean getFromDialog()
{
     filename  = dialogFilename.value();
     return super();
}

public container pack()
{
    return [#CurrentVersion,#CurrentList];
}

public void run()
{
 

    try
    {
        this.vendUpdate();
    }
    catch (Exception::UpdateConflict)
    {
        throw Exception::UpdateConflict;
    }

}

public boolean unpack(container packedClass)
{
    int version = conPeek(packedClass,1);
    #localmacro.ListV1
     #endmacro
    ;

    switch(version)
    {
        case #CurrentVersion:
            [version,#CurrentList] = packedClass;
            break;

        default:
            return false;
    }

    return true;
 }
 protected void vendUpdate()
{
    TextBuffer               textBuffer   = new TextBuffer();
    SysOperationProgress     importProgress = new SysOperationProgress();
    #AviFiles
    container                dataCon;
    Io                       importFile;
    #file
    str                      Delimiter = ",";
    boolean                  firstRow = true;
    int                      totalNumOfRows,totalnumOfRecordRows;
    int                      counterNumOfRows = 1;
    
    str                      filePath,fileNameOnly;
    filename                 fileType;
    
    InteropPermission        permission;
    permission = new InteropPermission(InteropKind::ClrInterop);
    importFile = new CommaTextIo(filename,#IO_Read);

    permission.assert();
    [filePath, fileNameOnly, fileType] = fileNameSplit(filename);

    if((!importFile) || (importFile.status() != IO_Status::Ok))         
       throw error ("No file selected");                               KEEPS throwing this error when running in batch.

    if(fileType != #csv)
        throw error ("Selected filetype is not *.csv");

    if(fileNameOnly != 'UpdateVend')
        throw error ("Selected CSV file is not UpdateVend");

    importFile.inFieldDelimiter(Delimiter);

    try
    {
      while(importFile.status() ==  IO_Status::Ok)
      {
      //main logic
      }
    }
    
    catch
    {
    }

}
}

Thanks

Mav

  • Verified answer
    Sergei Minozhenko Profile Picture
    Sergei Minozhenko 23,083 on at
    RE: Unable to get filename while running class in batch mode.

    Hi Mav,

    1. It's a standard file select dialog and I'm not sure if you can hide folders (at least I haven't done this before) and I'm not sure if 2 csv files will be located in different folders, how a user can navigate between the folders?

    2. You don't need to change run on to the server, otherwise, users will not be able to import the file without batch

    Probably you can utilize the validate method and when all checks are passed you can use WinApi::copyFile to copy file to file share and store new filepath to FileShareName variable which should be added to pack\unpack list and utilize it as filename when you read the file in run method.

    public boolean validate(Object calledFrom = null)
    {
        boolean ret = super(calledFrom);
        
        //validate if selected file exist
        
        if (ret)
        {
            //If in batch copy file
            if (this.batchInfo().parmBatchExecute())
            {
                fileShareName = ... //generate from share folder   file name
            
                ret = WinApi::copyFile(filename, fileShareName)
                
                if (!ret)
                {
                    ret = checkFailed("Something went wrong while copying file to share");
                }
            }
            //If not in batch just copy value of filname to fileShareName
            else
            {
                fileShareName = filename;
            }
        }
        
        
        return ret;    
    }
    

  • Mav Profile Picture
    Mav on at
    RE: Unable to get filename while running class in batch mode.

    Hi BR, Sergie,

    1. Changing to [tag:AllFilesName] shows up CSV files + folders of the directory.

    Is there a way that it should only show CSV files in the  directory & no folders from that directory.

    2. Selected file is locally present in users system.

    Are you suggesting that class's runOn property has to be changed to Server from Called from & code should be modified in a way that it does the following.

    a>Copies the user selected file in dialog to network share on AOS drive location

    b>Processes this file from the network share.

    Do you have any sample /reference code for copying locally selected file to network share & then instructing the program to process same file from network share along, if yes can you please share.

    Thanks

    Mav

  • Suggested answer
    Sergei Minozhenko Profile Picture
    Sergei Minozhenko 23,083 on at
    RE: Unable to get filename while running class in batch mode.

    Hi Mav,

    1. You need to change #AllFiles macros (.*) to #AllFilesName (*)

    dialog.filenameLookupFilter(["CSV files", #AllFilesName   #csv]);

    2. Where is the file located when the user selects it? A batch job is running on the server machine and if a user selects a local file, your code will try to find the same file in the same location but on the server machine. Usually, you need to use a file share or copy file before the operation is scheduled from the local client to the shared folder.

Helpful resources

Quick Links

Replay now available! Dynamics 365 Community Call (CRM Edition)

Catch up on the first D365 Community Call held on 7/10

Community Spotlight of the Month

Kudos to Saurav Dhyani!

Congratulations to the June Top 10 community leaders!

These stars go above and beyond . . .

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 287,989 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,588 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans