web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Active Discussion

[Resolved] Error when editing a PO line "Cannot create a record in Relationship between the purchase order line and the inventory transactions originator"

(0) ShareShare
ReportReport
Posted on by 47

Sharing this fix with the community.  This is a very unusual bug that happened randomly. 

Symptom:

Editing one of these three columns in the PO line screen.

pastedimage1625073614304v2.png

Threw this error..."Cannot create a record in Relationship between the purchase order line and the inventory transactions originator (InventTransOriginPurchLine). Inventory transactions originator: XXX, XXX. The record already exists"

pastedimage1625073632805v3.png

Cause:

Native AX code relies on an exception to determine when to "insert" or "update" record.  Relying on an exception is not a good practice. 

Resolution:

I overhauled the problematic method InventTransOriginPurchLine->writeOriginOwnerRelationship().

Below has more info. 

2021_2D00_06_2D00_28-infolog-bug-fix.png

Copy/paste the code below into table method InventTransOriginPurchLine->writeOriginOwnerRelationship(). Side note...I tried pasting this code into a "code bloc" but the forum mangled the post. 


/// ★★★★★★★★★★★★
/// 2021-06-28 Overhauled method.
/// Bug happened randomly when updating fields Price, Qty, Delivery Date.  AX would throw error message "Cannot create a record in Relationship between the purchase order line and the inventory transactions originator (InventTransOriginPurchLine). Inventory transactions originator: XXX, XXX. The record already exists" -RR
/// The native method was poorly written. Native method relied on the insert command failing (then throwing an exception) to determine if need to "update" instead.
/// The whole method is overhauled. 
/// ★★★★★★★★★★★★
public static server void writeOriginOwnerRelationship(
    DataAreaId              _purchLineDataAreaId,
    InventTransId           _purchLineInventTransId,
    InventTransOriginId     _inventTransOriginId)
{
    InventTransOriginPurchLine  inventTransOriginPurchLine;
    Counter                     logCount = infologLine();

    //Used to count records. -RR
    InventTransOriginPurchLine inventTransOriginPurchLine_TempRR;

    //Original method relied on the insert command throwing an exception to know if needed to update (bad code).
    //New method below checks if record exists.  So no need for exception handling. -RR
    select count(RecId)
    from inventTransOriginPurchLine_TempRR
    where
        1==1
        && inventTransOriginPurchLine_TempRR.PurchLineDataAreaId        == _purchLineDataAreaId
        && inventTransOriginPurchLine_TempRR.PurchLineInventTransId     == _purchLineInventTransId
        && inventTransOriginPurchLine_TempRR.InventTransOrigin          == _inventTransOriginId
        ;

    //Debug: Lines counted
    //info(int2str(inventTransOriginPurchLine_TempRR.RecId));

    ttsBegin;
    //No records exist, so insert.
    //In this context, property RecId = record count.  -RR
    if (inventTransOriginPurchLine_TempRR.RecId == 0)
    {
        inventTransOriginPurchLine.PurchLineDataAreaId      = _purchLineDataAreaId;
        inventTransOriginPurchLine.PurchLineInventTransId   = _purchLineInventTransId;
        inventTransOriginPurchLine.InventTransOrigin        = _inventTransOriginId;
        inventTransOriginPurchLine.insert();

        //Debug:
        //info("inserted");
    }
    //Record exists, so update.
    else
    {
        //Side note...I don't see the point of this code block.  The updates I have tested did not change the InventTransOriginId column value.
        //Also, once the InventTransOrigin record is created, I believe it is meant to last for life.
        //Anyhow, will leave it in for the heck of it. -RR
       update_recordset inventTransOriginPurchLine
       setting InventTransOrigin = _inventTransOriginId
       where inventTransOriginPurchLine.PurchLineInventTransId  == _purchLineInventTransId
           && inventTransOriginPurchLine.PurchLineDataAreaId    == _purchLineDataAreaId
           && inventTransOriginPurchLine.InventTransOrigin      != _inventTransOriginId
           ;

        //Debug:
        //info("updated");
    }

    ttsCommit;

}

Usual disclaimer...Use above at your own risk. 

  • huijij Profile Picture
    19,813 on at

    Thanks for your sharing.

    regards

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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Abhilash Warrier Profile Picture

Abhilash Warrier 463 Super User 2026 Season 1

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 423 Super User 2026 Season 1

#3
Subra Profile Picture

Subra 392

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans