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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Unanswered

How to exclude prepayment accounts from the vendor account statement report using x++

(3) ShareShare
ReportReport
Posted on by 406
Hello,
 
for the vendor account statement report, i've added a boolean field "Exclude Prepayments". i want to make it that if this field is true, to exclude all the invoices of type "Prepayment". So what i did first is extended the contract class and added the parameter. then, i extended  the Classes\VendAccountStatementIntDP\initVendTransQuery method to add a range based on this parameter value. but it's still not working till now. the vendor invoice type is "InvoiceType" field in VendInvoiceJour table, and has Enum PurchInvoiceType::VendorAdvance (for the value prepayment).
 
here is my code if anyone can tell me whats wrong, i will appreiciate it.
 
 
[ExtensionOf(classStr(VendAccountStatementIntDP))]
final class VendAccountStatementIntDP_Extension
{
    private NoYes ExclPrepayments;
    public void processReport()
    {
        // Call the base class method
        next processReport();
        // Retrieve the contract object
        VendAccountStatementIntContract vendAccountStatementIntContract = this.parmDataContract();
    
        // Get the value of the exclusion parameter
        ExclPrepayments = vendAccountStatementIntContract.parmExclPrepayment();
    
        // Initialize the query
        Query query = this.initVendTransQuery();
    }
    public Query initVendTransQuery()
    {
        Query query = next initVendTransQuery();
        // Initialize the base query as done in the standard DP class
        QueryBuildDataSource vendTransDataSourceBase = SysQuery::findOrCreateDataSource(query, tableNum(VendTrans), tableNum(VendTable));
       
        // Add VendInvoiceJour as a child data source of VendTrans
        QueryBuildDataSource vendInvoiceJourDataSource = vendTransDataSourceBase.addDataSource(tableNum(VendInvoiceJour));
        vendInvoiceJourDataSource.relations(true);
        vendInvoiceJourDataSource.joinMode(JoinMode::OuterJoin);
        // Define correct linking
        vendInvoiceJourDataSource.addLink(fieldNum(VendInvoiceJour, LedgerVoucher), fieldNum(VendTrans, Voucher));
        vendInvoiceJourDataSource.addLink(fieldNum(VendInvoiceJour, InvoiceAccount), fieldNum(VendTrans, AccountNum));
        // Add filtering condition based on the parameter value
        if (ExclPrepayments == NoYes::Yes)
        {
            vendInvoiceJourDataSource.addRange(fieldNum(VendInvoiceJour, InvoiceType)).value(SysQuery::valueNot(Enum2Str(PurchInvoiceType::VendorAdvance)));
        }
        return query;
    }
}

 
  • Martin Dráb Profile Picture
    241,637 Most Valuable Professional on at
    First of all, note that the call of enum2Str() isn't needed and it may even be a bug.
     
    If it doesn't help, your next step should be debugging. For example, verify that the executed query is the query you want. If not, you know that you need to look at how the query is constructed (e.g. whether a previously-used query isn't used instead of your new query). If the query is what you intended but it doesn't give you the right result, then you have a problem in the design of your query.
  • D365FO DEV Profile Picture
    406 on at
    Hi Martin,
     
    Could please have a look on my code, and if anything is wrong, you can provide me the correct code based on my requirement. 
    i will appreciate it.
     
    thanks
  • Martin Dráb Profile Picture
    241,637 Most Valuable Professional on at
    In addition to what I've already told you, I see that you're using OuterJoin, therefore your query never filters VendTrans records. If you wanted to filter VendTrans records, then your query is wrong. You should test your query before using it as a building block of a bigger solution.
     
    Also, the fact that you're using both relations(true) and addLink() looks wrong. Because the list of fields in your addLink() calls isn't correct, throw that away and let relations(true) do its job. You really should look at what query you've generated from your code (instead of just checking the records returned by the query); you'd be able to easily review things like join conditions.
  • D365FO DEV Profile Picture
    406 on at
    I've changes the code based on what you told me. Is it possible you provide the updated code please?  as i am still a junior developer and finding this challenging, thanks martin alot!
  • Martin Dráb Profile Picture
    241,637 Most Valuable Professional on at
    I can't write code without knowing how you want it to work. You need to decide what you want to achieve before writing code, and then test whether every part of code works correctly.
     
    Note that you haven't reacted to the remark about the type of join, so we have no new information to work with.
     
    Also, you said you already changed the code, therefore you should share it with us. I'm not keen to duplicate your work; I'd rather focus on problems.
  • D365FO DEV Profile Picture
    406 on at
    Hi martin, what i need exactly is that when the boolean field is True, the report should not show the invoices of type "Prepayment". thats it.

    Moreover, find below my updated code version:

    [ExtensionOf(classStr(VendAccountStatementIntDP))]
    final class VendAccountStatementIntDP_Extension
    {
        private NoYes ExclPrepayments;  
        public void processReport()
        {
            
            next processReport();
           
            VendAccountStatementIntContract vendAccountStatementIntContract = this.parmDataContract();         
            ExclPrepayments = vendAccountStatementIntContract.parmExclPrepayment();          
            Query query = this.initVendTransQuery();       
            //if (ExclPrepayments == NoYes::Yes)
            //{
            //    
            //}
        }
        public Query initVendTransQuery()
        {
            Query query = next initVendTransQuery(); 
            QueryBuildDataSource vendTransDataSourceBase = SysQuery::findOrCreateDataSource(query, tableNum(VendTrans), tableNum(VendTable));
            
            QueryBuildDataSource vendInvoiceJourDataSource = vendTransDataSourceBase.addDataSource(tableNum(VendInvoiceJour));
            vendInvoiceJourDataSource.relations(true); 
            vendInvoiceJourDataSource.joinMode(JoinMode::InnerJoin);  
            
            if (ExclPrepayments == NoYes::Yes)
            {
                vendInvoiceJourDataSource.addRange(fieldNum(VendInvoiceJour, InvoiceType)).value(SysQuery::valueNot(Enum2Str(PurchInvoiceType::VendorAdvance)));
            }
            return query;
        }
    }

  • Martin Dráb Profile Picture
    241,637 Most Valuable Professional on at
    Please start by fixing the very first thing I mentioned. Then test your code and if it doesn't work correctly, debug it. First of all, check whether the query used to fetch the data is the query you wanted to generate. But I'm already repeating myself...
     
    If debugging isn't doesn't tell you what's wrong and you need our help, explain the problem to us and tell us what you found when you debugged your code.
  • D365FO DEV Profile Picture
    406 on at
    I debugged the code, and the root cause is that there is 2 different data sources and are not linked.
    thats why it's not working. it is possible you update the code to make it work? as im facing some challenges.
     
     
    [ExtensionOf(classStr(VendAccountStatementIntDP))]
    final class VendAccountStatementIntDP_Extension
    {
        private NoYes ExclPrepayments;  
        public void processReport()
        {
            
            next processReport();
           
            VendAccountStatementIntContract vendAccountStatementIntContract = this.parmDataContract();         
            ExclPrepayments = vendAccountStatementIntContract.parmExclPrepayment();          
            Query query = this.initVendTransQuery();       
          //  if (ExclPrepayments == NoYes::Yes)
         //   {
                
          //  }
        }
        public Query initVendTransQuery()
        {
            Query query = next initVendTransQuery(); 
            QueryBuildDataSource vendTransDataSourceBase = SysQuery::findOrCreateDataSource(query, tableNum(VendTrans), tableNum(VendTable));       
            QueryBuildDataSource vendInvoiceJourDataSource = vendTransDataSourceBase.addDataSource(tableNum(VendInvoiceJour));
            vendInvoiceJourDataSource.relations(true); 
            vendInvoiceJourDataSource.joinMode(JoinMode::InnerJoin);  
        
            
            if (ExclPrepayments == NoYes::Yes)
            {
                vendInvoiceJourDataSource.addRange(fieldNum(VendInvoiceJour, InvoiceType)).value(SysQuery::valueNot(Enum2Str(PurchInvoiceType::VendorAdvance)));
            }
            return query;
        }
    }
     
  • Martin Dráb Profile Picture
    241,637 Most Valuable Professional on at
    Please be more specific. Which data sources aren't linked?
     
    Consider sharing the SQL query with us.

    By the way, I think that your call SysQuery::findOrCreateDataSource(query, tableNum(VendTrans), tableNum(VendTable)) is wrong. Is there are valid scenario when you want to add an additional VendTrans data source? If not, don't do it! Use something like query.dataSourceTable(tableNum(VendTrans)) instead.
  • D365FO DEV Profile Picture
    406 on at
    
    
    {SELECT * FROM VendInvoiceJour(VendInvoiceJour_1) WHERE VendTrans.Voucher = VendInvoiceJour.LedgerVoucher AND VendTrans.AccountNum = VendInvoiceJour.InvoiceAccount AND VendTrans.TransDate = VendInvoiceJour.InvoiceDate}
    VendInvoiceJour and VendTrans aren't linked.

    Here are the SQL QUeries:

    {SELECT * FROM VendTrans(VendTrans_1) WHERE VendTable.AccountNum = VendTrans.AccountNum AND ((TransDate>={ts '2023-01-01 00:00:00.000'} AND TransDate<={ts '2024-01-01 00:00:00.000'})) OUTER JOIN * FROM TransactionReversalTrans(TransactionReversalTrans_1)
    ON VendTrans.RecId = TransactionReversalTrans.RefRecId AND VendTrans.TableId = TransactionReversalTrans.RefTableId NOTEXISTS JOIN * FROM TransactionReversalTrans(TransactionReversalTrans_2) WHERE VendTrans.TableId = TransactionReversalTrans.RefTableId AND
    VendTrans.RecId = TransactionReversalTrans.RefRecId AND ((Reversed = 1))}

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 Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

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

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 284 Super User 2026 Season 2

#2
Aayush Tiwari Profile Picture

Aayush Tiwari 213

#3
Subra Profile Picture

Subra 200 Super User 2026 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans