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

Difficulty to get date difference

(1) ShareShare
ReportReport
Posted on by 2,430
Hi Experts,
 
I am facing difficulty to get difference (workflowTrackingTableNew.CreatedDateTime - workflowTrackingTable.CreatedDateTime) in X++ code. I do not find in which place I should put. I can get this in SSRS expression but problem is if have to sum up (workflowTrackingTableNew.CreatedDateTime - workflowTrackingTable.CreatedDateTime) the divide number of purchase order. SSRS expression not allowed me.  The last column expression is =DateDiff("d",Fields!PRWFDate.Value,Fields!POWFDate.Value)
 
 
 
 
 public void processReport()
        {   
            contract = this.parmDataContract();
            TransDate reportDate = contract.parmFromDate();
            VendGroupId vendGroup = contract.parmVendGroup();
            Timezone daxTimeZone = DateTimeUtil::getUserPreferredTimeZone();

            PurchReqTable purchReqTable;
            PurchTable purchTable;
            PurchReqLine purchReqLine;
            PurchLine purchLine;
            VendGroup vendGroupTable;
            WorkflowTrackingTable workflowTrackingTable, workflowTrackingTableNew;
            WorkflowTrackingStatusTable workflowTrackingStatusTable, workflowTrackingStatusTableNew;
            WorkflowTrackingCommentTable workflowTrackingCommentTable, workflowTrackingCommentTableNew;

            integer poCount;
            AmountMST dateDiffSum,totalDayDiff;

            delete_from tmpTable;

            // Loop through purchase requisitions within the specified date range
            while select PurchReqId, CreatedDateTime
    from purchReqTable
    where purchReqTable.RequisitionStatus == PurchReqRequisitionStatus::Closed
       && (purchReqTable.CreatedDateTime >= datetobeginUtcDateTime((reportDate - 7), daxTimeZone)
       && purchReqTable.CreatedDateTime <= datetoendUtcDateTime(reportDate, daxTimeZone))
            {
                tmpTable.clear();
                tmpTable.PRId = purchReqTable.PurchReqId;
                tmpTable.PRDate = DateTimeUtil::date(purchReqTable.CreatedDateTime);

                // Fetch workflow tracking for purchase requisitions
                while select RecId, User, CreatedDateTime, TrackingContext
        from workflowTrackingTable Order by CreatedDateTime desc
                where workflowTrackingTable.TrackingType == WorkflowTrackingType::Approval
        exists join workflowTrackingStatusTable
              where workflowTrackingStatusTable.RecId == workflowTrackingTable.WorkflowTrackingStatusTable
              && workflowTrackingStatusTable.ContextRecId == purchReqTable.RecId
              && workflowTrackingStatusTable.ContextTableId == tableNum(PurchReqTable)
                {
                    tmpTable.PRUser = workflowTrackingTable.User;
                    tmpTable.PRWFDate = workflowTrackingTable.CreatedDateTime;
                    tmpTable.PRTrackingContext = workflowTrackingTable.TrackingContext;
                }

                // Loop through purchase requisition lines to find corresponding purchase orders
                while select PurchId
        from purchReqLine
        where purchReqLine.PurchReqTable == purchReqTable.RecId
                {
                    // Find corresponding purchase order
                    select PurchId, CreatedDateTime
            from purchTable
            where purchTable.PurchId == purchReqLine.PurchId
               exists join vendGroupTable
                 where vendGroupTable.VendGroup == purchTable.VendGroup
                    && vendGroupTable.VendGroup == vendGroup;
                    {

                       
                        dateDiffSum = DateTimeUtil::date(purchTable.CreatedDateTime) - DateTimeUtil::date(purchReqTable.CreatedDateTime);
                    if(purchTable.RecId>0)
                    {
                        // Calculate the difference in days between PO and PR creation dates
                        if ((vendGroup == 'LOCAL' && dateDiffSum > 0) || (vendGroup == 'FOREIGN' && dateDiffSum > 0))
                       // if ((vendGroup == 'LOCAL') || (vendGroup == 'FOREIGN'))
                        {
                            tmpTable.POId = purchTable.PurchId;
                            tmpTable.PODate = DateTimeUtil::date(purchTable.CreatedDateTime);
                            tmpTable.DayDiff = dateDiffSum;

                            // Fetch workflow tracking for purchase orders
                            while select RecId, User, CreatedDateTime, TrackingContext
                    from workflowTrackingTableNew order by CreatedDateTime asc
                                where workflowTrackingTableNew.TrackingType == WorkflowTrackingType::Submission
                    exists join workflowTrackingStatusTableNew
                          where workflowTrackingStatusTableNew.RecId == workflowTrackingTableNew.WorkflowTrackingStatusTable
                          && workflowTrackingStatusTableNew.ContextRecId == purchTable.RecId
                          && workflowTrackingStatusTableNew.ContextTableId == tableNum(PurchTable)
                            {
                                tmpTable.POUser = workflowTrackingTableNew.User;
                                tmpTable.POWFDate = workflowTrackingTableNew.CreatedDateTime;
                                tmpTable.POTrackingContext = workflowTrackingTableNew.TrackingContext;
                                //  tmpTable.TotalDayDiff = workflowTrackingTable.CreatedDateTime;
                            }
                        }
                    }
                    }
                }

                tmpTable.insert(); // Insert the record after processing all data
            }

        }
Thanks,
Faiz
  • Martin Dráb Profile Picture
    240,748 Most Valuable Professional on at
    The only place where you have workflowTrackingTableNew available is the last while select; the block of code on lines 82-85.
     
    A strange thing that you execute a while loop and keep overwriting the same fields. Effectively, you get values from the last record; fetching all the other records is a waste of resources.
  • faiz7049 Profile Picture
    2,430 on at
    Thank you Martin,
     
    I have update the X++ code but value is not in same format dateDiffSum and UTCDayDiff. Both type is real.
     
    How to correct it in X++ code.
     
     
     
     
      dateDiffSum = DateTimeUtil::date(purchTable.CreatedDateTime) - DateTimeUtil::date(purchReqTable.CreatedDateTime);
                        if(purchTable.RecId>0)
                        {
                            // Calculate the difference in days between PO and PR creation dates
                            if ((vendGroup == 'LOCAL' && dateDiffSum > 0) || (vendGroup == 'FOREIGN' && dateDiffSum > 0))
                           // if ((vendGroup == 'LOCAL') || (vendGroup == 'FOREIGN'))
                            {
                                tmpTable.POId = purchTable.PurchId;
                                tmpTable.PODate = DateTimeUtil::date(purchTable.CreatedDateTime);
                                tmpTable.DayDiff = dateDiffSum;
    
                                // Fetch workflow tracking for purchase orders
                                select RecId, User, CreatedDateTime, TrackingContext
                        from workflowTrackingTableNew order by CreatedDateTime asc
                                    where workflowTrackingTableNew.TrackingType == WorkflowTrackingType::Submission
                        exists join workflowTrackingStatusTableNew
                              where workflowTrackingStatusTableNew.RecId == workflowTrackingTableNew.WorkflowTrackingStatusTable
                              && workflowTrackingStatusTableNew.ContextRecId == purchTable.RecId
                                    && workflowTrackingStatusTableNew.ContextTableId == tableNum(PurchTable);
                                {
                                    tmpTable.POUser = workflowTrackingTableNew.User;
                                    tmpTable.POWFDate = workflowTrackingTableNew.CreatedDateTime;
                                    tmpTable.POTrackingContext = workflowTrackingTableNew.TrackingContext;
                                    tmpTable.UTCDayDiff=DateTimeUtil::date(workflowTrackingTableNew.CreatedDateTime)-DateTimeUtil::date(workflowTrackingTable.CreatedDateTime);
                                    //  tmpTable.TotalDayDiff = workflowTrackingTable.CreatedDateTime;
                                }
                            }
     
  • Martin Dráb Profile Picture
    240,748 Most Valuable Professional on at
    Please tell us more about the problem.
    Are you saying that you're calculating the value incorrectly, or that you've assigned the correct value to tmpTable.UTCDayDiff and you just want to format the number differently in SSRS?
  • faiz7049 Profile Picture
    2,430 on at
    Hi Martin,
     
    Problem was workflowTrackingTable.CreatedDateTime picking wrong date inside loop. I stored it in local variable PRWFDate then use in loop.
    Now "Total day" date difference is based on date. I want this based on DateTime. If you see 1st row below screenshot "Total days diff with time" =0 but "Total day" =1.00
     
    How to include time as well.
     
     
     
     
     
    while select RecId, User, CreatedDateTime, TrackingContext
            from workflowTrackingTable Order by CreatedDateTime desc
                    where workflowTrackingTable.TrackingType == WorkflowTrackingType::Approval
            exists join workflowTrackingStatusTable
                  where workflowTrackingStatusTable.RecId == workflowTrackingTable.WorkflowTrackingStatusTable
                  && workflowTrackingStatusTable.ContextRecId == purchReqTable.RecId
                    && workflowTrackingStatusTable.ContextTableId == tableNum(PurchReqTable)
                    {
                        tmpTable.PRUser = workflowTrackingTable.User;
                        tmpTable.PRWFDate = workflowTrackingTable.CreatedDateTime;
                        tmpTable.PRTrackingContext = workflowTrackingTable.TrackingContext;
                        PRWFDate=workflowTrackingTable.CreatedDateTime;
                    }
    
                    // Loop through purchase requisition lines to find corresponding purchase orders
                    while select PurchId
            from purchReqLine
            where purchReqLine.PurchReqTable == purchReqTable.RecId
                    {
                        // Find corresponding purchase order
                        select PurchId, CreatedDateTime
                from purchTable
                where purchTable.PurchId == purchReqLine.PurchId
                   exists join vendGroupTable
                     where vendGroupTable.VendGroup == purchTable.VendGroup
                        && vendGroupTable.VendGroup == vendGroup;
                        {
    
                           
                            dateDiffSum = DateTimeUtil::date(purchTable.CreatedDateTime) - DateTimeUtil::date(purchReqTable.CreatedDateTime);
                        if(purchTable.RecId>0)
                        {
                            // Calculate the difference in days between PO and PR creation dates
                            if ((vendGroup == 'LOCAL' && dateDiffSum > 0) || (vendGroup == 'FOREIGN' && dateDiffSum > 0))
                           // if ((vendGroup == 'LOCAL') || (vendGroup == 'FOREIGN'))
                            {
                                tmpTable.POId = purchTable.PurchId;
                                tmpTable.PODate = DateTimeUtil::date(purchTable.CreatedDateTime);
                                tmpTable.DayDiff = dateDiffSum;
    
                                // Fetch workflow tracking for purchase orders
                                select RecId, User, CreatedDateTime, TrackingContext
                        from workflowTrackingTableNew order by CreatedDateTime asc
                                    where workflowTrackingTableNew.TrackingType == WorkflowTrackingType::Submission
                        exists join workflowTrackingStatusTableNew
                              where workflowTrackingStatusTableNew.RecId == workflowTrackingTableNew.WorkflowTrackingStatusTable
                              && workflowTrackingStatusTableNew.ContextRecId == purchTable.RecId
                                    && workflowTrackingStatusTableNew.ContextTableId == tableNum(PurchTable);
                                {
                                    tmpTable.POUser = workflowTrackingTableNew.User;
                                    tmpTable.POWFDate = workflowTrackingTableNew.CreatedDateTime;
                                    tmpTable.POTrackingContext = workflowTrackingTableNew.TrackingContext;
                                    tmpTable.TotalDayDiff=workflowTrackingTableNew.CreatedDateTime-workflowTrackingTable.CreatedDateTime;
                                    tmpTable.UTCDayDiff=DateTimeUtil::date(workflowTrackingTableNew.CreatedDateTime)-DateTimeUtil::date(PRWFDate);
                                    
                                    
                                }
                            }
                        }
                        }
                    }
    
                    tmpTable.insert(); // Insert the record after processing all data
                }
    
    Thanks,
    Faiz
  • Martin Dráb Profile Picture
    240,748 Most Valuable Professional on at
    You explicitly drop the time by DateTimeUtil::date(). This method takes a date/time value and returns a date only.
  • faiz7049 Profile Picture
    2,430 on at
    Hi Martin,
     
    How to get with time. Please
  • Verified answer
    Martin Dráb Profile Picture
    240,748 Most Valuable Professional on at
    You can use DateTimeUtil::getDifference() to get a difference between two date/time values. The result is the number of seconds.
     
    Another approach is utilizing the .NET base class library, namely Subtract() method of System.DateTime. This will give you a System.TimeSpan object, which has properties like Days.
    #TimeConstants
    utcDateTime a = DateTimeUtil::newDateTime(mkDate(1,9,2024), 8*#SecondsPerHour);
    utcDateTime b = DateTimeUtil::newDateTime(mkDate(5,9,2024), 13*#SecondsPerHour);
    
    System.DateTime bNet = b;
    System.TimeSpan diff = bNet.Subtract(a);
    info(strFmt("%1 days, %2 hours", diff.Days, diff.Hours));

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

Congratulations to the July Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Supply chain | Supply Chain Management, Commerce

#1
André Arnaud de Calavon Profile Picture

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

#2
Aayush Tiwari Profile Picture

Aayush Tiwari 71

#3
Paolo Cecchelli Profile Picture

Paolo Cecchelli 69 Super User 2026 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans