I can't update one record in table while inserting these records in another table

ttsbegin;
Select Forupdate table1
where recId=X;
table2.field1=table1.field;
table1.field=X;
table2.field2=table1.field;
table2.insert();
table1.doUpdate;
ttscommit;

insert part is successful, but doupdate doesn’t work. if I’m doing something wrong please do share or if here’s a better way, also please do share.

P.S. table1 is where the update is happening but table2 is where I want to track these changes

First of all, always describe the problem. Just saying that it doesn’t work isn’t sufficient. Did you get any error? If so, please share it with us. Also, what did you find when you tried to debug your code?

Let me format your code and repost it with Insert > Insert Code. Please do the same next time.

ttsbegin;

select forUpdate table1
	where RecId = X;

table2.field1 = table1.field;
table2.field2 = table1.field;
table2.insert();

table1.field = X;
table1.doUpdate;

ttscommit;

Unfortunately this is not valid X++ code. Could you please share your actual code?

By the way, I see that you want to update table1 without checking if the select statement found anything. It would fail in such a case.

By the way, don’t prefix tags with # and always attach a version tag (e.g. AX 2012 or D365FO) when creating a new thread.

 ttsbegin;

                select forUpdate table1
                    where table1.RecId==5637149222;
               
                if (table1)
                {    
                    
                    table2.Table                = tableId2Name(table1.TableId);
                    table2.RefRecID             = table1.RecId;
                    table2.Field                = 'AvailOrdered';
                    table2.OldValue             = strFmt('%1',table1.AvailOrdered);
                
                    table1.AVAILORDERED         = 1;
                    
                    table2.NewValue             = strFmt('%1', table1.AvailOrdered);
                    table2.RequestNumber        = 'request Num';
                    table2.ShortDescription     = 'correction';
                    
                    
                    
                    table2.insert();
                    
                    table1.doUpdate();

 

                    ttsCommit;
                }
                else
                    error("Table wasn't selected");
            }

 

            Info("Wrong company");
          }
 
    }

 

}

I get error not while compiling, but after clicking on menuitem that runs runnable class: Cannot edit a record in On-hand inventory (table1). Cannot call NEXT, update(), or delete() on buffer where data is selected or inserted in another transaction scope

Let me fix indentation ofn your code before continuing:

ttsbegin;

select forUpdate table1
	where table1.RecId == 5637149222;

if (table1)
{    
	table2.Table                = tableId2Name(table1.TableId);
	table2.RefRecID             = table1.RecId;
	table2.Field                = 'AvailOrdered';
	table2.OldValue             = strFmt('%1',table1.AvailOrdered);

	table1.AVAILORDERED         = 1;
	
	table2.NewValue             = strFmt('%1', table1.AvailOrdered);
	table2.RequestNumber        = 'request Num';
	table2.ShortDescription     = 'correction';
	
	table2.insert();
	
	table1.doUpdate();

	ttsCommit;
}
else
{
	error("Table wasn't selected");	
}

A huge bug is that you you leave the transaction open if table1 wasn’t found.

And it sounds like you’re trying to update InventSum, which is horrible idea. You’re on a way to destroy data consistency of a critical system table. Please stop immediately.

Please tell us what you’re trying to achieve from the business point of view, so we can suggest technical design that won’t break your (or your client’s) system.

I thought of creating a new table where I could track every change runnable classes would make, but I’ve chosen wrong table to practice on I guess. IWhy is Inventsum wrong table for this?

InventSum is maintained by the system based on various inventory movements and such things. It’s pretty complicated, both because the business logic itself is complicated and because it’s a performance-critical table and there are some tricks to to improve performance. If you update it directly, the record in InventSum will stop being a sum of related inventory transactions, i.e. information about stock will became inconsistent.

If I want to try something and I don’t want to bother creating a new table for it, I often use CustGroup. It’s a simple table with (usually) just a few records.

Got it!
Thank you!