Save changes even if error is raised.

Hello,

I have a code which sends a report to each Customer and inserts the date of the last e-mail into a field in the Customer table.

The problem is that there are some cases where errors appear in the middle of the process and the transaction is rolled back(Unfortunately i can’t filter them all out).

Let’s take an example:

I select 1000 Customers and try to send the report and mark the field “Last date”

When at Customer nr 516 I have a error then for no Customer the “Last date” field is saved because the whole transaction is rolled back.

How to prevent this?

I tried to figure it out by myself but couldn’t find a way.

Customer.RESET 
Customer.SETRANGE("No.","No.");
IF Customer.FINDFIRST THEN REPEAT

    ///Here is the part with the report creation and sending on mail\\

    Customer."Last date" := TODAY;

    Customer.MODIFY(TRUE);

UNTIL Customer.NEXT = 0;

best regards,

Hi,

You can split the code into two functions. First Function to Send Email and Second Function to Update the Customer table once first function runs successfully.

Like this?

Create a function for example with name ModifyFunction and just put it into the code?

Customer.RESET 
Customer.SETRANGE("No.","No.");
IF Customer.FINDFIRST THEN REPEAT

    ///Here is the part with the report creation and sending on mail\\

    Customer."Last date" := TODAY;

    ModifyFunction();

UNTIL Customer.NEXT = 0;

best regards,

Hi Thomas,

Although I generally tell people not to use commit, then this is actually a situation where it should be used.

Customer.RESET 
Customer.SETRANGE("No.","No.");
IF Customer.FINDFIRST(TRUE,FALSE) THEN 
  REPEAT
    ///Here is the part with the report creation and sending on mail\\
    Customer."Last date" := TODAY;
    Customer.MODIFY(TRUE);
    COMMIT;
  UNTIL Customer.NEXT = 0;

Hello, Erik.

I’ve tried function and it worked!

I’m ashamed that i couldn’t find it by myself. It was kind of obvious.

I tried all kind of searches on google except “commit dynamics nav”. . .

I’m aware that this function is dangerous and can do a lot of mess when used in the wrong situations.

best regards,

Happy that it worked for you.

The reason why COMMIT is dangerous is if you put it into the “wrong” place, then you get transactions committed, which should have “rolled back” on failure. In your case you do the COMMIT after you “transaction” (printing the report), so you’re pretty safe.

PS: Also notice another difference: FINDSET(TRUE,FALSE) as supposed to just FINDSET. Since you are modifying the customer, then you should say that in the command.