Skip to main content

Notifications

Announcements

No record found.

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 735

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)

  • How to post multiple sales orders as a single invoice using code? - collective invoice
    Hi Shooowtek,
     
    did you solve the problem? I have the same problem to post multiple sales in one invoice.
  • Deepak Agarwal Profile Picture
    Deepak Agarwal 8,564 on at
    RE: How to post multiple sales orders as a single invoice using code? - collective invoice

    You need to debug this to see what query build in code after all the qbr and than try to amend your code accordingly.

  • Shooowtek Profile Picture
    Shooowtek 735 on at
    RE: How to post multiple sales orders as a single invoice using code? - collective invoice
    [quote user="Deepak Agarwal"]

    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.

    [/quote]

    Hello agarwal AX , I followed your suggestions and unfortunately the problem still exists....

  • Suggested answer
    Deepak Agarwal Profile Picture
    Deepak Agarwal 8,564 on at
    RE: How to post multiple sales orders as a single invoice using code? - collective invoice

    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.

  • Martin Dráb Profile Picture
    Martin Dráb 230,466 Most Valuable Professional on at
    RE: How to post multiple sales orders as a single invoice using code? - collective invoice

    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.

  • Shooowtek Profile Picture
    Shooowtek 735 on at
    RE: How to post multiple sales orders as a single invoice using code? - collective invoice

    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];
        }

  • Shooowtek Profile Picture
    Shooowtek 735 on at
    RE: How to post multiple sales orders as a single invoice using code? - collective invoice
    [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.

  • Deepak Agarwal Profile Picture
    Deepak Agarwal 8,564 on at
    RE: How to post multiple sales orders as a single invoice using code? - collective invoice

    Btw why this line is commented

      //_salesId = "SO11214,SO11215,SO11216"

    as you are using this in qbr.

  • Martin Dráb Profile Picture
    Martin Dráb 230,466 Most Valuable Professional on at
    RE: How to post multiple sales orders as a single invoice using code? - collective invoice

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

  • Shooowtek Profile Picture
    Shooowtek 735 on at
    RE: How to post multiple sales orders as a single invoice using code? - collective invoice
    [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();

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

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,735 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,466 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans