Create a record between ttsbegin and ttscommit

Hi All,

In our functionality, a program was written to create sales order. A validation was done on sales price. If the sales price for an item is zero, an error was being thrown. This process is done under ttsbegin and ttscommit. Now the requirement is to save the details of sales orders which encounter this particular error. For this I have written a code which inserts a record in customized table just before the error is thrown. The issue is, after the error is thrown the record created is not available. How can I resolve this. The schenario is as below

ttsbegin;

create sales order id;

fetch item id;

init sales order from invent table;

set sales price

ttsbegin

create record in customized table

ttscommit;

insert sales line;

ttscommit;

Regards,

Raghav.

ttsbegin and ttscommit is not like braces ‘{’, ‘}’. if you open that after insert or update you have to close.

I found 2 ttsbegin and 2 ttscommit in your code.

pls do it like

ttsbegin

updation code

ttscommit

ttsbegin

updation code

ttscommit

like this only you have to do. it should not overlap

Hey Maria,

The program is written under single ttsbegin and ttscommit. The code which I have written has overlap as

below

ttsbegin

ttsbegin

ttscommit

ttscommit.

However, if I remove the inner tts statements, The record which Is created is not available after the error is thrown, since the ttscommit is not being executed.

Regards,

Raghav.

Hi raghav,

Use ‘try’ and ‘catch’ blocks. Insert into your customized table in the catch block like,

try

{

create sales order;

if(failed)

{

throw error;

}

}

catch

{

insert into your table;

}

  • Arun.

can you pls share your code and the error what you are getting…

Hi Arun,

I appreciate your logic. Unfortunately, I cannot use your logic since our program is standard functionality. In-fact, the transaction is called under try catch.

try

{

ttsbegin;

program to create sales order;

ttscommit;

}

catch(exception::error)

{

error;

}

Maria,

The program is large. The code cannot be put in this post. So, I’ve given the the scenario.

I found table.ttscommit() method. This works fine, but the sales order is created. The sales order should not be created. Record should be created in customized table and the error should be thrown.

Regards,

Raghavendra.

Hey,

I think, I found the solution. Correct me if I’m wrong.

The logic along with fix is as follow

try

{

ttsbegin;

program to create sales order and sales order line; (Here sales order will be created and sales order line creation is under process)

check for sales price.

if (validation fails)

{

salesTable.ttsabort();

CustomTable.ttsbegin();

initialize custom table;

customTable.insert()

customTable.ttscommit();

throw error(unit price is zero);

}

continue with existing logic;

}

catch

{

error;

}

Regards,

Raghav.

Nested transaction scopes won’t help you in any way - the outmost transaction will be used (and rollbacked), the inner transactions have almost no effect. You have several options:

  1. Validate the price before entering any transaction. If the price is zero, log the error, otherwise call the logic creating orders. It’s important to be aware that price can be set to 0 after the validation succeeded, but it’s often acceptable, because it’s unlikely.
  2. Use the transaction, but before throwing the error, save additional information (ItemId, for example) to a variable and use it in the catch block (i.e. after the transaction is rollbacked) to create the log record. This is what I would probably use in you situation.
  3. Use a separate connection with it’s own transaction scope - find an example on my blog: dev.goshoom.net/en/2011/03/independent-transaction-in-ax/

Hi,

I used the third solution.

First option is not valid for our functionality, because the client requirement is to throw error if sales price is zero.

Section option, I couldn’t use because the class where try catch used is a standard class. If I modify this class, it will affect for other functionality of ours.

Third option, suits my requirement. It works fine.

Regards,

Raghavendra.