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

Notifications

Announcements

No record found.

Community site session details

Community site session details

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

X++ job Print Current and Previous year Sales

(0) ShareShare
ReportReport
Posted on by 1,883

Dear all,

I'm executing following job to print Current and Previous year Sales and its not returning any value. Please check below code and advice.

static void custInvoiceSum(Args _args)
{

int                     currentYear = year(systemDateGet());
int                     previousYear = currentYear - 1; // previous year
int                     nextYear = currentYear   1; // Next year
// I have another way to get previous year and next year. But, this will match the exact date in the previous year and next year.

//date                     prvyear = prevYr(systemDateGet());
//date                     nextyears = nextYr(systemDateGet());

String30                    CurrentYearStart = "01" "/" "01" "/" int2Str(currentYear);
String30                     CurrentYearEnd = "12" "/" "31" "/" int2Str(currentYear);
 String30                     previousYearStart = "01" "/" "01" "/" int2Str(previousYear);
  String30                      previousYearEnd = "12" "/" "31" "/" int2Str(previousYear);

 CustInvoiceJour custInvoiceJourPrvYear,  custInvoiceJourCurrentYear;
 SalesTable salesTable;   

    //Invoiceamount previous year
 select sum(InvoiceAmount) from custInvoiceJourPrvYear where custInvoiceJourPrvYear.InvoiceDate >= str2Date(previousYearStart, 213)
&& custInvoiceJourPrvYear.InvoiceDate <= str2Date(previousYearEnd, 213)
&& custInvoiceJourPrvYear.InvoiceAccount == salesTable.InvoiceAccount
&& custInvoiceJourPrvYear.InvoiceAmount > 0.00;

    select sum(InvoiceAmount) from custInvoiceJourCurrentYear where custInvoiceJourCurrentYear.InvoiceDate >= str2Date(CurrentYearStart, 213)
&& custInvoiceJourCurrentYear.InvoiceDate <= str2Date(CurrentYearEnd, 213)
&& custInvoiceJourCurrentYear.InvoiceAccount == salesTable.InvoiceAccount
&& custInvoiceJourCurrentYear.InvoiceAmount > 0.00;
    
     
     info(strFmt("Previous Year %1 ", custInvoiceJourPrvYear.InvoiceAmount));
    info(strFmt("Current Year %1 ", custInvoiceJourCurrentYear.InvoiceAmount));



}

I have the same question (0)
  • Verified answer
    Gunjan Bhattachayya Profile Picture
    35,423 on at

    Hi Fagru,

    You have declared a table buffer for SalesTable but haven't assigned anything to it.

    In the query you are you have a condition to look for totals corresponding to salesTable.InvoiceAccount. Since this field will always be null, the queries won't return any value. You can try removing this condition from the where clause and check if you get any values.

    Additionally, you can use mkDate function for the date variables instead of using string variables. For an example,

    InvoiceDate currentYearStart = mkDate(1, 1, currentYear);

  • Faqruddin Profile Picture
    1,883 on at

    Hello Gunjan,

    Thank you. Its working. But how can I do following.

    ->Starting of Current month and Ending of Current month

    ->Starting of Previous month and Ending of Previous month

  • Verified answer
    Gunjan Bhattachayya Profile Picture
    35,423 on at

    You can use the functions dateStartMnth and endMnth for start date of month and end of current month.

    Date currDate = systemDateGet();
    Date currStartMth, currEndMth;
    
    currStartMth = dateStartMth(currDate);
    currEndMth   = endMth(currDate);

    For previous month you can use prevMnth function using the current date. Then you can use the afore mentioned functions for start and end dates of previous month.

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

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

#1
Abhilash Warrier Profile Picture

Abhilash Warrier 669 Super User 2025 Season 2

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 449 Super User 2025 Season 2

#3
Martin Dráb Profile Picture

Martin Dráb 384 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans