Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

While Try catch

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

hi everyone. ithis may be a strange request but i'm really curious about this issue(if it can be called like that) that I have.

as a context, I'm reading an input file to make some insertions in the database.

while coding, seemed to me a good idea to read the file and if a line is wrong then skip it giving a warning about what's wrong in which line and read the next one, of it's ok then make the insert. after came the idea "wait, what if there is 500 lines and just one it's ok... (been extreme) that will be confusing for the user about what's ok and what's not or which lines where inserted cause that will show up a really big Infolog... that's gonna be more troublesome than helpful" so it's better to check the file and if something it's wrong then don't insert anything.

but I'm not sure what approach should I use... I mean there is more than one way of doing it, is there any "best practice" of how to make this?  a way to see what's more effective? or it doesn't really matter since the size file ain't that big to cause any performance problem at all no matter the way i choose to do the charge.

cause right now i have two ways of doing this in my mind:

the Current way(which i already said it may go wrong in certain circumstances):

read the records from the file, validate the data and if it's ok then insert, if not then go to the next record, the lines that are wrong goes to the info log to be fixed and loaded in another file.

another way 1:

read the records from the File, validate all the lines of the file, if ok then read the file again and make the insert to the database, if there is an error in the data put it in the info log to fix it and reload the file

another way 2:

read the records from the file, validate the data and if it's ok then do the insert, if not, then rollback previous insertions, don't allow inserts for the next lines and continue just checking the file for error so they can be fixed, errors will be thrown in the info log.

another way 3(came to me while writing this, a variation of my current process):

My current Process, just instead of throwing various errors in the info log put them in an output file, which will contain exactly the input line(s) of error, plus one last column with the warning that error that I was displaying in the info log in the first time.

as part of this issue, if iIuse the next syntax:

try

ttsbegin;

while

{

//read current line

//insert info into database with an  ax class

}

ttscommit;

catch

{

throw error("bad information");

}

if there is an error let's say on the last line of 100, the 99  inserted records are rollbacked? I'm aware that the "throw error" does a rollback, but I'm not sure if this rollback works correctly if the insertions are been made inside a while loop, I ca't Find an example like this in the documentation.

another thing I'd like to ask (a bit related to this) is

I'm Using classes from ax to make the insertion to the database, (axInventJournalTrans in this case) what's the purpose of this kind of classes in ax? what's the difference between this and a regular insert into SQL clause?

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: While Try catch

    thanks for your time :) i'll give it a try

  • Mea_ Profile Picture
    Mea_ 60,278 on at
    RE: While Try catch

    Yes. But that's only my opinion. 

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: While Try catch

    second option is better then?

  • Mea_ Profile Picture
    Mea_ 60,278 on at
    RE: While Try catch

    Yes, you got the idea.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: While Try catch

    hi ievgen thanks for your reply.

    sorry for not been specifing in this part. this is how i'm working with the file:

    While(InFile.status() == IO_Status::Ok)

    {

       copy the data into a container

       validate the data from the container

       if ok insert record in table

       if not show what's wrong in what line

    }

    is this what you mean by putting into a container or is better this way? (this is how i'm picturing it at least)

    While(InFile.status() == IO_Status::Ok)

    {

       copy the data into a container (with an extra field to mark if info it's ok or not)

       Validate the data from the container

    }

    after you copy all file data into the container

    while(read the container "line by line")

    {

       if data ok then insert

       if not show what's wrong in what line;

    }

  • Verified answer
    Mea_ Profile Picture
    Mea_ 60,278 on at
    RE: While Try catch

    Hi luis torres,

    First of all, let answer on transaction question. If you have any code inside of the transaction that throws Exception::Error, it will rollback the transaction and it does not matter if has a loop or not.

    Now, generally it's a bad idea to read file line by line and insert because file IO could be slow and you creating big transaction for a long period of time. To avoid this usually you want to read it lets say to a container or a list, validate data and then insert into a table. You can validate and insert only valid records or stop process after first invalid record, but it's more about your business requirements than best practice.

    Ax classes are used by AIF and parm* and set* method could have some logic, to init some fields base on other fields, you can use them or assign values directly to table fields and invoke all required methods on your own.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: While Try catch

    sorry for the long post btw

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,321 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans