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

Notifications

Announcements

Community site session details

Community site session details

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

How to post multiple sales orders as a single invoice using code? - collective invoice

(0) ShareShare
ReportReport
Posted on by 843

hi,

I am trying to create an invoice from multiple sales orders. Unfortunately, without success. Could someone look at the code and suggest how to solve this?

[SysODataAction('InVoice posting', false), SysODataCollectionAttribute('_salesId', Types::String)]
    public static str CTS_PostInvoice(str _salesId)
    {
        //_salesId = "SO11214,SO11215,SO11216"
        
        salesFormLetter FormLetter;
        FormLetter = SalesFormLetter::construct(DocumentStatus::Invoice);

        Query query = new Query(queryStr(MySalesTableQuery));
        QueryBuildDataSource qbd = query.dataSourceTable(tableNum(SalesTable));
        QueryBuildRange qbr = qbd.addRange(fieldNum(SalesTable, SalesId));
        qbr.value(_salesId);

        SysQueryRun queryRun = new SysQueryRun(query);

        FormLetter.chooseLinesQuery(queryRun);
        FormLetter.transDate(systemdateget());
        FormLetter.specQty(SalesUpdate::PackingSlip);
        FormLetter.sumBy(AccountOrder::Account);
        FormLetter.showQueryForm(false);
        FormLetter.printFormLetter(true);
        FormLetter.chooseLines();
        FormLetter.run();

551273.png

I checked in the debugger and the problem occurs in the FormLetter.chooseLines() line;

debug error message:

Microsoft.Dynamics.Ax.Xpp.ErrorException
HResult=0x80131500
Message=Update has been canceled.
Source=Dynamics.AX.ApplicationSuite
StackTrace:
at Dynamics.AX.Application.FormletterParmData.`catchAll()
at Dynamics.AX.Application.FormletterParmData.catchAll()
at Dynamics.AX.Application.FormletterParmData.`createData(Boolean _append, Boolean @_append_IsDefaultSet)
at Dynamics.AX.Application.FormletterParmData.createData(Boolean _append, Boolean @_append_IsDefaultSet)
at Dynamics.AX.Application.FormletterParmData.createData(Boolean _append)
at Dynamics.AX.Application.SalesFormletterParmDataInvoice.`createData(Boolean _append, Boolean @_append_IsDefaultSet)
at Dynamics.AX.Application.SalesFormletterParmDataInvoice.createData(Boolean _append, Boolean @_append_IsDefaultSet)
at Dynamics.AX.Application.SalesFormletterParmDataInvoice.createData(Boolean _append)
at Dynamics.AX.Application.SalesFormletterParmData.`chooseLinesFromContract(Object[ _inputContract)
at Dynamics.AX.Application.SalesFormletterParmData.chooseLinesFromContract(Object[ _inputContract)
at Dynamics.AX.Application.SalesFormLetter.`chooseLines(Boolean _append, Boolean _directDeliveryUpdate, Boolean @_append_IsDefaultSet, Boolean @_directDeliveryUpdate_IsDefaultSet)
at Dynamics.AX.Application.SalesFormLetter.SalesFormLetterCoCHelper.`chooseLines(SalesFormLetter instance, Boolean arg0, Boolean arg1, Boolean , Boolean )
at Dynamics.AX.Application.SalesFormLetterRevRec_Extension.chooseLines(SalesFormLetter this, Boolean _append, Boolean _directDeliveryUpdate)
at Dynamics.AX.Application.SalesFormLetter.SalesFormLetterCoCHelper.`chooseLines(SalesFormLetter instance, Boolean arg0, Boolean arg1, Boolean , Boolean )
at Dynamics.AX.Application.SalesFormLetter.chooseLines(Boolean _append, Boolean _directDeliveryUpdate, Boolean @_append_IsDefaultSet, Boolean @_directDeliveryUpdate_IsDefaultSet) 

at Dynamics.AX.Application.CTS_SalesEntity.`CTS_PostInvoice(String _salesId) in xppSource://Source/xxx\AxDataEntityView_CTS_SalesEntity.xpp:line 73
at Dynamics.AX.Application.SalesFormLetter.chooseLines()
at Microsoft.Dynamics.Ax.Xpp.ReflectionCallHelper.MakeStaticCall(Type type, String MethodName, Object[ parameters)

I have the same question (0)
  • Martin Dráb Profile Picture
    238,601 Most Valuable Professional on at

    Which line of code throws the error "Wrong argument type for function"?

  • Shooowtek Profile Picture
    843 on at

    Hello Martin,

    That's what's strange to me and I can't locate it. When debugging, the only error I get appears on FormLetter.chooseLines() line..

    I didn't write before that this is a method that is run in the data entity.

    Are you able to help me?

  • Martin Dráb Profile Picture
    238,601 Most Valuable Professional on at

    Maybe you don't have debugging enabled for Application Suite package.

    By the way, do you have SalesLine data source in MySalesTableQuery?

  • Shooowtek Profile Picture
    843 on at
    [quote

    Maybe you don't have debugging enabled for Application Suite package.

    By the way, do you have SalesLine data source in MySalesTableQuery?

    [/quote

    How to enable debugging for Application Suite package?

    I changed the query to SalesUpdate and unfortunately, the same error appears...

    [SysODataAction('InVoice posting', false), SysODataCollectionAttribute('_salesId', Types::String)]
        public static str CTS_PostInvoice(str _salesId)
        {
            //_salesId = "SO11214,SO11215,SO11216"
            
            salesFormLetter FormLetter;
            FormLetter = SalesFormLetter::construct(DocumentStatus::Invoice);
    
            Query query = new Query(queryStr(SalesUpdate));
            QueryBuildDataSource qbd = query.dataSourceTable(tableNum(SalesTable));
            QueryBuildRange qbr = qbd.addRange(fieldNum(SalesTable, SalesId));
            qbr.value(_salesId);
    
            SysQueryRun queryRun = new SysQueryRun(query);
    
            FormLetter.chooseLinesQuery(queryRun);
            FormLetter.transDate(systemdateget());
            FormLetter.specQty(SalesUpdate::PackingSlip);
            FormLetter.sumBy(AccountOrder::Account);
            FormLetter.showQueryForm(false);
            FormLetter.printFormLetter(true);
            FormLetter.chooseLines();
            FormLetter.run();

  • Martin Dráb Profile Picture
    238,601 Most Valuable Professional on at

    In Visual Studio, open Dynamics 365 > Options. Switch to Debugging tab and configure it according to your needs.

  • Deepak Agarwal Profile Picture
    8,602 on at

    Btw why this line is commented

      //_salesId = "SO11214,SO11215,SO11216"

    as you are using this in qbr.

  • Shooowtek Profile Picture
    843 on at
    [quote user="Deepak Agarwal"]

    Btw why this line is commented

      //_salesId = "SO11214,SO11215,SO11216"

    as you are using this in qbr.

    [/quote]

    This is the argument that this method receives when it is called.

    Thanks.

  • Shooowtek Profile Picture
    843 on at

    goshoom As per your suggestion, I ran the debugger and now I get an error on below line in the chooseLines method (SalesFormLetter class)
    container outputContainer = SalesFormletterParmData::chooseLinesFromContract(formLetterParmDataChooseLinesContract.pack());

    public void  chooseLines(
            boolean _append = false,
            boolean _directDeliveryUpdate = false)
        {
            this.parmDirectDeliveryUpdate(_directDeliveryUpdate);
    
            FormletterParmDataChooseLinesContract formLetterParmDataChooseLinesContract = FormletterParmDataChooseLinesContract::construct();
            formLetterParmDataChooseLinesContract.parmFormletterContractPacked(this.packDataContract(salesFormLetterContract));
            formLetterParmDataChooseLinesContract.parmAppend(_append);
            formLetterParmDataChooseLinesContract.parmReuseParmUpdate(this.parmId() ? true : false);
    
            container outputContainer = SalesFormletterParmData::chooseLinesFromContract(formLetterParmDataChooseLinesContract.pack());
    
            FormLetterParmDataOutputContract outputContract = this.unpackFormletterParmDataOutputContract(outputContainer);
    
            this.getValuesFormletterParmData(outputContract);
        }

    chooseLinesFromContract:

    public static container chooseLinesFromContract(container _inputContract)
        {
            FormletterParmDataChooseLinesContract contract = FormletterParmDataChooseLinesContract::construct();
            contract.unpack(_inputContract);
    
            // 
            boolean countryRegion_RU = SysCountryRegionCode::isLegalEntityInCountryRegion([#isoRU]);
            // 
            SalesFormletterParmData salesFormletterParmData = SalesFormletterParmData::newChooseLines(contract.parmFormletterContractPacked(),
                                                                                                      contract.parmReuseParmUpdate(),
                                                                                                      contract.parmOnlyCreateParmUpdate(),
                                                                                                      contract.parmExecuteLateSelection());
            // 
            if (countryRegion_RU)
            {
                salesFormletterParmData.parmStorno_RU(CustParameters::find().CreditError);
                salesFormletterParmData.parmStornoPhysical_RU(InventParameters::find().StornoPhysicalPosting_RU);
            }
            // 
            salesFormletterParmData.createData(contract.parmAppend());
    
            // 
            #EECountryRegionCodes
            if (SysCountryRegionCode::isLegalEntityInCountryRegion(#easternEuropeAllandRU))
            {
                salesFormletterParmData.initOfficials_RU();
            }
            // 
            // 
            if (SysCountryRegionCode::isLegalEntityInCountryRegion([#isoRU, #isoCZ, #isoHU, #isoPL]))
            {
                salesFormletterParmData.checkAdvancePayments_W();
            }
            // 
    
            // 
            if (TaxParameters::isEasternEuropeOrVatDueDateFeatureEnabled())
            {
                salesFormletterParmData.initDates_W(null);
            }
            // 
    
            return salesFormletterParmData.getOutputContractPacked();
        }

    FormletterParmDataChooseLinesContract:

    #define.CurrentVersion(1)
        #LOCALMACRO.ParmList
            formletterContractPacked,
            append,
            reuseParmUpdate,
            onlyCreateParmUpdate,
            executeLateSelection
        #ENDMACRO
    
            public container pack()
        {
            return [#CurrentVersion,#ParmList];
        }

  • Martin Dráb Profile Picture
    238,601 Most Valuable Professional on at

    If you're saying that the error is throw inside either pack() or chooseLinesFromContract(), step inside in the debugger and take a look where.

    Alternatively, you can put a breakpint to Info.add() to stop when a message is added to infolog.

  • Suggested answer
    Deepak Agarwal Profile Picture
    8,602 on at

    Have you checked how your query build after all ranges, it seems issue with your range value. Try with single value first. If thats working that you need see how to supply multiple values is its not working with comma, try to add multiple ranges something like below

    qbd.addRange(fieldNum(SalesTable, SalesId)).value('SO11214');

    qbd.addRange(fieldNum(SalesTable, SalesId)).value(SO11215);

    You also need to break your parameter SalesId in such way to get individual values.

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…

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

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

#1
André Arnaud de Calavon Profile Picture

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

#2
Abhilash Warrier Profile Picture

Abhilash Warrier 287 Super User 2026 Season 1

#3
Martin Dráb Profile Picture

Martin Dráb 225 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans