AX 2012 - Update must be performed inside a transaction

Hi Readers,

Many times you might have seen below error while you were updating or inserting data using X++.

Error:

Update must be performed inside a transaction


Solution:

The solution for the above error is you must keep your updating or inserting code into ttsBegin and ttsCommit.

Syntax:

ttsBegin;

// Your_Code
ttsCommit;

Example:



Copy code from below:

// Code written by Chirag Gupta
static void updateRebateData(Args _args)
{
    PdsRebateTable          pdsRebateTable;
    PdsRebateAgreement      pdsRebateAgreement;
    PdsRebateAgreementLine  pdsRebateAgreementLine;
    int                     counter = 0;
   
    while select forUpdate pdsRebateAgreementLine
        join pdsRebateAgreement
            where pdsRebateAgreementLine.RebateAgreementRefRecId == pdsRebateAgreement.RecId
               && pdsRebateAgreement.PdsRebateType               == 'Ad-Hoc'
               && pdsRebateAgreement.PdsCustRebateRelation       == 'C00489'
               && pdsRebateAgreementLine.RebateAmtType           == PdsRebateAmtType::Percentage
               && pdsRebateAgreementLine.dataAreaId              == curext()
    {
        if (pdsRebateAgreementLine)
        {
            ttsBegin;
            pdsRebateAgreementLine.RebateValue = 11;
            pdsRebateAgreementLine.doUpdate();
            counter++;
            ttsCommit;
        }
    }
   
    info(strFmt("%1", counter));
    info("Done");
}


Comment below if this solution helps you to resolve your issue...

Author:
Chirag Gupta
Microsoft Dynamics 365 AX Technical Consultant at IBM Bangalore

Date:
24-Sep-2019

Happy Learning !!

Comments

Popular posts from this blog

[Solved] : Error: String or binary data would be truncated. Cannot execute a data definition language command on Vendor (VendLedgerReconciliationTmp)

AX 2012 - Command prompt compilation / AxBuild.exe for Parallel Compile on AOS of X++ to P-code / Parallel Compilation

AX 2012 - How to send SSRS report in email as an attachment using X++

AX 2012 - How to stop AOS when services got stuck at stopping status

Error: There was no endpoint listening at net.tcp://your_server_name:8201/Dynamics AX/Services…

AX 2012 - How to make a form field mandatory

Fetch vendor address using X++ in AX 2012

[Error] - Error in unit of amount in currency KES. Must be rounded to 0.01 | The unit of 37.563.5343100000000000 KES being posted to account 32710001-- is outside the current penny rounding threshold | Posting has been canceled

AX 2012 - How to print current date and time on SSRS Report

For more information about this error navigate to the report server on the local server machine, or enable remote errors in AX 2012