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

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested Answer

How to copy and create new SalesLine record using x++?

(0) ShareShare
ReportReport
Posted on by 843

Hello,

I want to create functionality to copy a SalesLine record.

I have a problem with creating a new record. I have tried several ways, but there is always an error, e.g: "Item number cannot be changed when item transactions have been generated. Delete line and re-create it with the new item number."

Could someone please suggest how to create a new SalesLine record?

        //create new salesLine
        ttsbegin;
        salesLineNew.clear();
        salesLineNew.initValue();
        salesLineNew.initFromSalesTable(SalesTable::find(salesLineLocal.SalesId));
        salesLineNew.SalesId = _salesId;
        salesLineNew.ItemId = _itemId;
        salesLineNew.SalesQty = _qty;
        salesLine.createLine(
        true,                                                       // validation
        true,                                                       // initFromSalesTable
        true,                                                       // initFromInventTableModule
        true,                                                       // calcInventQty
        true,                                                       // searchMarkup
        true,                                                       // searchPrice
        SalesTable::find(salesLineLocal.SalesId).Reservation == ItemReservation::Automatic,      // checkReservation
        false,                                                      // skipCreditLimitCheck
        false,                                                      // ignoreSalesTableInventDim
        true,                                                       // setLineNum
        SalesTable::find(salesLineLocal.SalesId).MatchingAgreement != 0);                        // searchAgreementLine
        ttscommit;

I have the same question (0)
  • Shooowtek Profile Picture
    843 on at
    RE: How to copy and create new SalesLine record using x++?

    //create new salesLine
            ttsbegin;
            salesLineNew.clear();
            salesLineNew.initValue();
            salesLineNew.initFromSalesTable(SalesTable::find(salesLineLocal.SalesId));
            salesLineNew.SalesId = _salesId;
            salesLineNew.ItemId = _itemId;
            salesLineNew.SalesQty = _qty;
            salesLineNew.createLine(
            true,                                                       // validation
            true,                                                       // initFromSalesTable
            true,                                                       // initFromInventTableModule
            true,                                                       // calcInventQty
            true,                                                       // searchMarkup
            true,                                                       // searchPrice
            SalesTable::find(salesLineLocal.SalesId).Reservation == ItemReservation::Automatic,      // checkReservation
            false,                                                      // skipCreditLimitCheck
            false,                                                      // ignoreSalesTableInventDim
            true,                                                       // setLineNum
            SalesTable::find(salesLineLocal.SalesId).MatchingAgreement != 0);                        // searchAgreementLine
            ttscommit;

  • Martin Dráb Profile Picture
    237,668 Most Valuable Professional on at
    RE: How to copy and create new SalesLine record using x++?

    Don't you have an existing line in salesLineNew?

  • Shooowtek Profile Picture
    843 on at
    RE: How to copy and create new SalesLine record using x++?

    SalesLineNew is new and I put the copied data there.

    I have done some debugging and the error appears on the insert in the createLineFromParameters method in SalesLine.

    this.insert(_salesLineCreateLineParameters.skipInventoryProcessing, _salesLineCreateLineParameters.searchMarkup, null, _salesLineCreateLineParameters.skipCreditLimitCheck, false, _salesLineCreateLineParameters.interCompanyInventTransId);

  • Shooowtek Profile Picture
    843 on at
    RE: How to copy and create new SalesLine record using x++?

    Can anyone suggest how to copy a salesLine record correctly?

  • Shooowtek Profile Picture
    843 on at
    RE: How to copy and create new SalesLine record using x++?

    I have a very similar problem to this link, but no combination of parameters solves the problem.

    https://community.dynamics.com/365/financeandoperations/f/dynamics-365-for-finance-and-operations-forum/357267/error-in-salesline-creation-using-x

    Can anyone help?

  • Shooowtek Profile Picture
    843 on at
    RE: How to copy and create new SalesLine record using x++?

    Doesn't anyone have any suggestions?

  • Martin Dráb Profile Picture
    237,668 Most Valuable Professional on at
    RE: How to copy and create new SalesLine record using x++?

    Your screenshot shows a completely different error than what you mentioned at the beginning. Please clarify this inconsistency.

  • Shooowtek Profile Picture
    843 on at
    RE: How to copy and create new SalesLine record using x++?

    With different combinations of parameters in the createLine() method, I get different error messages.

    error message: "Item number cannot be changed when item transactions have been generated. Delete line and re-create it with the new item number."

    "Update has been cancelled"

    salesLineNew.createLine(true, true, true, false, false, false, false, false, true);

    error message: "object reference not set to an instance of an object"

    salesLineNew.createLine(true,true,true,true,true,true);

        ttsbegin;
            salesLineLocal.selectForUpdate(true);
            salesLineLocal.SalesQty = salesLineLocal.SalesQty - _qty;
            salesLineLocal.update();
            ttscommit;
    
            //create new salesLine
            ttsbegin;
            salesLineNew.clear();
            inventDim.clear();
            salesLineNew.initValue();
            //buf2buf
            buf2Buf(salesLineLocal, salesLineNew);
    
            salesLineNew.initFromSalesTable(SalesTable::find(_salesId));
            salesLineNew.ItemId = _itemId;
            salesLineNew.SalesQty =  _qty;
            salesLineNew.SalesId = _salesId;
    
            //inventDim
            inventDim = InventDim::findOrCreate(inventDim);
            salesLineNew.InventDimId = inventDim.inventDimId;
    
            salesLineNew.createLine(true, true, true, false, false, false, false, false, true);
            //salesLineNew.createLine(true,true,true,true,true,true);
        ttscommit;

    I am confused completely and have no idea what the correct combination of parameters in createLine() should be....

  • Martin Dráb Profile Picture
    237,668 Most Valuable Professional on at
    RE: How to copy and create new SalesLine record using x++?

    Which parameters you should use depends on what logic you want to execute. Don't try it randomly - decide what you want and then debug that particular case.

  • Shooowtek Profile Picture
    843 on at
    RE: How to copy and create new SalesLine record using x++?

    I want to copy 1:1 an existing SalesLine record.

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

#2
André Arnaud de Calavon Profile Picture

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

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 398 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans