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 :
Supply chain | Supply Chain Management, Commerce
Answered

Getting only one value

(4) ShareShare
ReportReport
Posted on by 2,430
Hi Experts,
 
I am getting only one value in activeCust=1. Can you please tell me where I am doing wrong.
 
public void processReport()
    {
       
       
        contract = this.parmDataContract() as SFA_RetentionContract;
        TransDate transDate = contract.parmReportDate();
        FromDate oneYear = prevYr(transDate);
        ToDate reportDate = transDate;
        FromDate twoYear = prevYr(oneYear);
        CustTable custTable, custTableNew;
        CustInvoiceJour custInvoiceJour, custInvoiceJourNew;
        CustTrans custTrans;
        CustInvoiceTrans custInvoiceTrans, custInvoiceTransNew;
        CustInvoiceLine custInvoiceLine;
    
        Map customerInvoiceCount = new Map(Types::String, Types::Int64);
        Map customerFirstInvoiceDate = new Map(Types::String, Types::Date);
        integer activeCust = 0;
        integer newCondition = 0;
        Set firstPeriodCustomers = new Set(Types::String);
        Set secondPeriodCustomers = new Set(Types::String);

// Find the first invoice date for each customer
        
while select minof(InvoiceDate), InvoiceAccount from custInvoiceJour
            where custInvoiceJour.InvoiceDate >= twoYear
                    && custInvoiceJour.InvoiceDate <= oneYear
          exists join custTable
            where custTable.AccountNum==custInvoiceJour.InvoiceAccount && (custTable.CustGroup != "SCP" || custTable.CustGroup != "RMS" || custTable.CustGroup != "FAS")
               
                exists join custInvoiceTrans
                where custInvoiceTrans.InvoiceId == custInvoiceJour.InvoiceId
                    && (custInvoiceTrans.ItemId like '101*' || custInvoiceTrans.ItemId like '201*' || custInvoiceTrans.ItemId like '301*' || custInvoiceTrans.ItemId like '251*')

            {

                customerFirstInvoiceDate.insert(custInvoiceJour.InvoiceAccount, custInvoiceJour.InvoiceDate);
            
}



          
        

//Counting number of invoices for each customer    


        while select InvoiceAccount,count(RecId) from custInvoiceJour
            where custInvoiceJour.InvoiceDate >= twoYear
                    && custInvoiceJour.InvoiceDate <= oneYear
          exists join custTable
            where custTable.AccountNum==custInvoiceJour.InvoiceAccount && (custTable.CustGroup != "SCP" || custTable.CustGroup != "RMS" || custTable.CustGroup != "FAS")
               
                exists join custInvoiceTrans
                where custInvoiceTrans.InvoiceId == custInvoiceJour.InvoiceId
                    && (custInvoiceTrans.ItemId like '101*' || custInvoiceTrans.ItemId like '201*' || custInvoiceTrans.ItemId like '301*' || custInvoiceTrans.ItemId like '251*')
        {

            customerInvoiceCount.insert(custInvoiceJour.InvoiceAccount,custInvoiceJour.RecId);

        }

        MapEnumerator mapEnumerator = customerInvoiceCount.getEnumerator();

        while (mapEnumerator.moveNext())
        {
            str accountNum = mapEnumerator.currentKey();
            Integer invoiceCount = mapEnumerator.currentValue();
            TransDate firstInvoiceDate = customerFirstInvoiceDate.lookup(accountNum);

            if(firstInvoiceDate != dateNull())

            {
                Integer numberOfMonths = (year(oneYear) - year(firstInvoiceDate)) * 12 + (mthOfYr(oneYear) - mthOfYr(firstInvoiceDate)) + 1; // Calculate months from first invoice date

                real invoiceRatio = (invoiceCount / numberOfMonths);

                if (invoiceRatio >= 0.5)
                {
                    firstPeriodCustomers.add(accountNum);
                }
            }
        }

        activeCust = firstPeriodCustomers.elements();
        tmpTable.ActiveCustomer = activeCust;

	tmpTable.insert();
}
 
 
Thanks,
Faiz
  • Martin Dráb Profile Picture
    241,656 Most Valuable Professional on at
    First of all, let me fix line indentation and throw away unused variables, to make your code easier to read:
    public void processReport()
    {
        contract = this.parmDataContract() as SFA_RetentionContract;
        TransDate transDate = contract.parmReportDate();
        FromDate oneYear = prevYr(transDate);
        FromDate twoYear = prevYr(oneYear);
        CustTable custTable;
        CustInvoiceJour custInvoiceJour;
        CustInvoiceTrans custInvoiceTrans;
    
        Map customerInvoiceCount = new Map(Types::String, Types::Int64);
        Map customerFirstInvoiceDate = new Map(Types::String, Types::Date);
        integer activeCust = 0;
        integer newCondition = 0;
        Set firstPeriodCustomers = new Set(Types::String);
        Set secondPeriodCustomers = new Set(Types::String);
    
        // Find the first invoice date for each customer            
        while select minof(InvoiceDate), InvoiceAccount from custInvoiceJour
            where custInvoiceJour.InvoiceDate >= twoYear
               && custInvoiceJour.InvoiceDate <= oneYear
            exists join custTable
                where custTable.AccountNum==custInvoiceJour.InvoiceAccount && (custTable.CustGroup != "SCP" || custTable.CustGroup != "RMS" || custTable.CustGroup != "FAS")
                exists join custInvoiceTrans
                    where custInvoiceTrans.InvoiceId == custInvoiceJour.InvoiceId
                       && (custInvoiceTrans.ItemId like '101*' || custInvoiceTrans.ItemId like '201*' || custInvoiceTrans.ItemId like '301*' || custInvoiceTrans.ItemId like '251*')
    
        {
            customerFirstInvoiceDate.insert(custInvoiceJour.InvoiceAccount, custInvoiceJour.InvoiceDate);
        }
    
        //Counting number of invoices for each customer    
        while select InvoiceAccount,count(RecId) from custInvoiceJour
            where custInvoiceJour.InvoiceDate >= twoYear
               && custInvoiceJour.InvoiceDate <= oneYear
            exists join custTable
                where custTable.AccountNum==custInvoiceJour.InvoiceAccount && (custTable.CustGroup != "SCP" || custTable.CustGroup != "RMS" || custTable.CustGroup != "FAS")
                exists join custInvoiceTrans
                    where custInvoiceTrans.InvoiceId == custInvoiceJour.InvoiceId
                       && (custInvoiceTrans.ItemId like '101*' || custInvoiceTrans.ItemId like '201*' || custInvoiceTrans.ItemId like '301*' || custInvoiceTrans.ItemId like '251*')
        {
            customerInvoiceCount.insert(custInvoiceJour.InvoiceAccount,custInvoiceJour.RecId);
        }
    
        MapEnumerator mapEnumerator = customerInvoiceCount.getEnumerator();
    
        while (mapEnumerator.moveNext())
        {
            str accountNum = mapEnumerator.currentKey();
            Integer invoiceCount = mapEnumerator.currentValue();
            TransDate firstInvoiceDate = customerFirstInvoiceDate.lookup(accountNum);
    
            if (firstInvoiceDate != dateNull())
            {
                Integer numberOfMonths = (year(oneYear) - year(firstInvoiceDate)) * 12 + (mthOfYr(oneYear) - mthOfYr(firstInvoiceDate)) + 1; // Calculate months from first invoice date
    
                real invoiceRatio = (invoiceCount / numberOfMonths);
    
                if (invoiceRatio >= 0.5)
                {
                    firstPeriodCustomers.add(accountNum);
                }
            }
        }
    
        activeCust = firstPeriodCustomers.elements();
        tmpTable.ActiveCustomer = activeCust;
    
        tmpTable.insert();
    }
  • Verified answer
    Martin Dráb Profile Picture
    241,656 Most Valuable Professional on at
    I think your forgot to test already the first select statement:
    // Find the first invoice date for each customer            
    while select minof(InvoiceDate), InvoiceAccount from custInvoiceJour
        where custInvoiceJour.InvoiceDate >= twoYear
           && custInvoiceJour.InvoiceDate <= oneYear
        exists join custTable
            where custTable.AccountNum==custInvoiceJour.InvoiceAccount && (custTable.CustGroup != "SCP" || custTable.CustGroup != "RMS" || custTable.CustGroup != "FAS")
            exists join custInvoiceTrans
                where custInvoiceTrans.InvoiceId == custInvoiceJour.InvoiceId
                   && (custInvoiceTrans.ItemId like '101*' || custInvoiceTrans.ItemId like '201*' || custInvoiceTrans.ItemId like '301*' || custInvoiceTrans.ItemId like '251*')
    
    {
        customerFirstInvoiceDate.insert(custInvoiceJour.InvoiceAccount, custInvoiceJour.InvoiceDate);
    }
    
    The comment says that it returns the invoice date for each customer, but it doesn't. It returns a single record with the minimum invoice date regardless of the customer; the InvoiceAccount field will always be empty. You apparently meant to use the 'group by' clause with InvoiceAccount, but you didn't and you haven't tested your code at all.
     
    You could easily see the problem if you walked through your code in the debugger.

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 > Supply chain | Supply Chain Management, Commerce

#1
André Arnaud de Calavon Profile Picture

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

#2
Laurens vd Tang Profile Picture

Laurens vd Tang 57 Super User 2026 Season 2

#3
Patrick Mouwen Profile Picture

Patrick Mouwen 48

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans