Skip to main content

Notifications

Finance | Project Operations, Human Resources, ...
Suggested answer

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

(0) ShareShare
ReportReport
Posted on by 737

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;

  • Suggested answer
    Komi Siabi Profile Picture
    Komi Siabi 12,759 Most Valuable Professional on at
    RE: How to copy and create new SalesLine record using x++?

    Hello,

    Instead of using crealteline() you can call the insert() on salesLine. Here is a sample code I have used few years back to create a sales line. 

    ttsbegin;
                salesLine.clear();
                salesLine.initValue();
                salesLine.SalesId               = salesTable.SalesId;
            
                salesLine.initFromSalesTable(salesTable);
                salesLine.CustomerLineNum       = 1;
                salesLine.LineNum               = 1;
                salesLine.modifiedFieldValue(fieldstr(salesLine,LineNum));
                salesLine.SalesUnit             = pmgParameters.SalesUnit;
                salesLine.SalesCategory         = pmgparameters.CapacitySalesCategory;
                salesLine.initFromItemOrCategory("",salesLine.SalesCategory,EcoResCategory::find(pmgparameters.CapacitySalesCategory).Name);
                salesLine.modifiedFieldValue(fieldstr(salesLine,SalesCategory));
                salesLine.SalesQty              = salesLine.PmgNetCapacity;
                salesLine.salesQtyModified();
                salesLine.PmgCapacityTariff     = pmgTariffCodes.ApplicableCapitalRecoveryRate   pmgTariffCodes.ApplicableFixedOMRate;
                salesLine.SalesPrice            = salesLine.PmgCapacityTariff;
                salesLine.LineAmount            = salesLine.SalesQty * salesLine.SalesPrice;
                salesLine.SalesStatus = SalesStatus::Backorder;
            
                salesLine.Name                      = strFmt("Capacity for the month of %1",PMGMonthlyDigitalReadingTable.PMGMonth);
                salesLine.insert();
    
                if(!salesLine.validateWrite())
                {
                    throw exception::Error;
                }
            
                SalesLineForeignTradeCategory.initValue();
                SalesLineForeignTradeCategory.initFromSalesLine(salesLine);
                SalesLineForeignTradeCategory.insert();
    
                ttscommit;

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

    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.

    [/quote]

    Is there an easier way to copy a SalesLine record?

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

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

  • Martin Dráb Profile Picture
    Martin Dráb 230,848 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
    Shooowtek 737 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
    Martin Dráb 230,848 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
    Shooowtek 737 on at
    RE: How to copy and create new SalesLine record using x++?

    Doesn't anyone have any suggestions?

  • Shooowtek Profile Picture
    Shooowtek 737 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
    Shooowtek 737 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
    Shooowtek 737 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);

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,979 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,848 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans