web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

Read data from text box x++ invalidfile name

(0) ShareShare
ReportReport
Posted on by 332

Hello Experts

I used this code to read a value from a text file, it worked fine in the local dev environment 

and in the sandbox gives me an error  invalidfile name

I got the code from 

community.dynamics.com/.../reading-data-from-tsv-file-using-x-in-d365fo

class ReadDataFromTSVFileJob
{        
    /// 

    /// Runs the class with the specified arguments.
    /// 

    /// The specified arguments.
    public static void main(Args _args)
    {
        TextIo file;
        FileName filename;
        container cont;
        FileIoPermission permission;
        boolean headerRow;
        #File
        
        try
        {
            //File name along with complete path
            filename = @"C:\TSVDataFile.txt";
            permission = new FileIoPermission(filename, #io_read);
            permission.assert();
            file = new TextIo(filename, #io_read);
            if (!file)
            throw Exception::Error;
            file.inRecordDelimiter(#delimiterCRLF);
            //inFieldDelimiter function splits the data where TAB is found because \t is passed as parameter and \t means tab.
            file.inFieldDelimiter("\t");           
            cont = file.read();
            //if headerRow is true, then it will not print the first line which is usually consists of Headings.
            headerRow = true;
            while (file.status() == IO_Status::Ok)
            {
                if(headerRow)
                {
                    headerRow=false;
                }
                else
                {
                    info(strfmt("%1 %2 %3 %4 %5 %6 %7 %8 %9",conpeek(cont,1),conpeek(cont,2),conpeek(cont,3),conpeek(cont,4),conpeek(cont,5),conpeek(cont,6),conpeek(cont,7),conpeek(cont,8),conpeek(cont,9)));
                }
                cont = file.read();
            }
        }
        catch(Exception::Error)
        {
            error("ERROR");
        }
        CodeAccessPermission::revertAssert();
    }
}

Any help 

I have the same question (0)
  • Verified answer
    GirishS Profile Picture
    27,827 Moderator on at

    Hi Ahmed Osama,

    File path you are specifying in the code is related to one environment. In your dev environment you have the file in C drive.

    In Sand box you won't have file in the C path. Thats why you are getting the error.

    I hope its related to D365 - In D365 you cannot hard code the file path since you are accessing the D365 from Web.

    Thanks,

    Girish S.

  • Ahmed Osama Ibrahim Profile Picture
    332 on at

    Thanks, I understood the issue

    is there any solution to achieve import from the txt file

  • Suggested answer
    GirishS Profile Picture
    27,827 Moderator on at

    Instead of hard coding the path - You can ask the user to upload the text file using dialog - Upon loading you can get the file and read data from the file.

    You can make use of "FileUploadTemporaryStorageResult" class to achieve this.

    Refer to the below blog.

    https://azd365.com/2022/01/06/how-to-read-text-file-in-dynamics-365-fo/

    Thanks,

    Girish S.

  • Ahmed Osama Ibrahim Profile Picture
    332 on at

    Thanks

    last q please

    can I import it automatically without browsing the folder ?

    I don't need extra steps

  • Verified answer
    Martin Dráb Profile Picture
    237,880 Most Valuable Professional on at

    Doing it automatically is possible, but it's a very different topic.

    You either need to put the file to a location that can accessed by the web server running in cloud, such as to an Azure storage, and somehow tell F&O how to find the right file.

    Or you need another process (such as Recurring Integrations Scheduler) to get the file and actively push it to F&O.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 611 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 529 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 285 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans