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, ...
Suggested Answer

'Cannot edit a record in Stores requisitions (XxxxxxxTable). Update must be performed inside a transaction.'

(0) ShareShare
ReportReport
Posted on by 507

Good day everyone,

I'm trying to update a custom table record as below, but I'm getting the above error. I have done build and sync but still the error is the same. My question is, is there anything that I'm missing here?

                ttsbegin;
                    _xxxxxTable = XxxxxxTable::find(xxxxx.StoresReqId);

                    if(_xxxxxTable)
                    {
                        _xxxxxTable.selectForUpdate();
                        _xxxxxTable.xxxxxDepartment = _attributeValue.getName();
                        _xxxxxTable.doUpdate();
                    }
                ttscommit;

pastedimage1664204464471v2.png

Thank you for your replies in advance.

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

    Hi Huggins Mafigu,

    Try the below code. You need to pass the argument to select for update.

                        _xxxxxTable = XxxxxxTable::find(xxxxx.StoresReqId);
    
                        if(_xxxxxTable)
                        {
                            ttsbegin;
                            _xxxxxTable.selectForUpdate(true);
                            _xxxxxTable.xxxxxDepartment = _attributeValue.getName();
                            _xxxxxTable.doUpdate();
                            ttscommit;
                        }
                    

    ,

  • Suggested answer
    Martin Dráb Profile Picture
    237,795 Most Valuable Professional on at

    If find() method is implemented correctly, it has a boolean parameter saying whether the record should be selected for update. You just need to set it to true. Throw away the call of selectForUpdate(), because it does nothing. Here is the new code:

    ttsBegin;
    
    XxxxxxTable xxxxxTable = XxxxxxTable::find(xxxxx.StoresReqId, true);
    
    if (xxxxxTable)
    {
    	xxxxxTable.xxxxxDepartment = _attributeValue.getName();
    	xxxxxTable.doUpdate();
    }
    
    ttsCommit;

    Unlike GirishS's code, this would work even with pessimistic locking.

    An alternative solution would be using update_recordset. Then you wouldn't even have to load the record from database.

    By the way, thing carefully whether you should use doUpdate() instead of update(). There may be important logic in update() that you'd skip. And if no such logic is there today, someone may add it tomorrow.

    Also, a good idea would be removing the extra line indentation before pasting code. Compare my code with code snippets above

  • Suggested answer
    Huggins Mafigu Profile Picture
    507 on at

    Thank you Martin and GirishS,

    At this point there is no other logic that I want to execute, that's why I chose

    .doUpdate()
    instead of
    .update()
    .

    But after applying this piece of code, I got the below error. Not sure if there is anything I'm missing

    pastedimage1664216595551v1.png

  • Suggested answer
    Komi Siabi Profile Picture
    13,093 Most Valuable Professional on at

    Hello,

    Based goshoom 's comment on your find method, I believe your find method is well defined. You need to have the table selected for update first of all.

    I would try something like this.

    You should try this. Here the table is selected for update.

    XxxxxxTable xxxxxTable;
    ttsbegin
    select forupdate xxxxxTable where xxxxxTable.recid == xxxxx.StoresReqId;
    
    if (xxxxxTable)
    {
    	xxxxxTable.xxxxxDepartment = _attributeValue.getName();
    	xxxxxTable.update();
    }
    
    ttsCommit;

  • Martin Dráb Profile Picture
    237,795 Most Valuable Professional on at

    I do see ttsbegin/ttscommit around your code, so I don't understand why it claims it's not there. Have you built the code?

    Your reasoning for skipping update() isn't good. You're basically saying that you have no business reason to skip the logic and it brings you no benefit. You're also saying that you're willing to potentially introduce a problem (such as data inconsistency), just because you aren't aware of a problem at the moment. Maybe there is none at the moment, but will all developers adding any update logic know that they must go to your code and change doUpdate() to update()? Of course not.

    And there already may be a problem, because the fact that there is no code in update() doesn't mean that no update logic exists. There could be CoC extension of update() or event handlers. Let me also quote Microsoft, if you don't believe me: "It's generally considered bad practice to use doUpdate, and we don't recommend that you use it." (Source:  X++ language reference / X++ data selection and manipulation / Update data).

  • Suggested answer
    Huggins Mafigu Profile Picture
    507 on at

    I have realized one thing from that code, Martin. It's correct, but when I was running this inside a Runnable class it was failing. Same code in the event handler it's working.

    I'm not sure what the problem is.

  • Martin Dráb Profile Picture
    237,795 Most Valuable Professional on at

    Will it make any difference if you use a select statement instead of find(), as Komi suggested? If so, it means that you have something wrong inside find() method.

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 660 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

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

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 307 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans