Implementing a Custom Date-Time Combination in Dynamics 365

 Implementing a Custom Date-Time Combination in Dynamics 365

In many business scenarios, especially in scheduling and calendar management, there's a need to combine separate date and time fields into a single date-time field. This article walks through a specific requirement in Microsoft Dynamics 365 where we combine a date-only field with two separate time fields to create comprehensive start and end date-time fields. This task requires careful handling of JavaScript within the Dynamics 365 environment.

The Requirement

Our goal is to combine:

  1. A date-only field (tri_sessiondate).
  2. Two text fields representing start (tri_scheduledstarttime) and end times (tri_scheduledendtime).

These combined values will populate two date-time fields (tri_schedulestartdateandtime and tri_scheduleenddateandtime).

Challenges

  • The time fields are text types and may include extra spaces.
  • The time fields need to be validated to ensure they contain valid time formats.

Solution Overview

We'll write a JavaScript function to handle this operation. The process includes:

  1. Sanitizing the time fields to remove extra spaces and validate their format.
  2. Combining the date and time values.
  3. Populating the target date-time fields.

Key JavaScript Concepts Used

  • Regular Expression (/\d/g): This is used to extract all digit characters from a string. In our context, it helps in extracting the hours and minutes from the time fields. The \d matches any digit (0-9), and the g flag denotes a global search, finding all matches rather than stopping after the first match.
  • parseInt and substring: parseInt(time.substring(0, 2)) is used to extract and convert the first two characters of the time string into an integer, representing the hours. Similarly, parseInt(time.substring(3, 5)) extracts and converts the next two characters for minutes.

The Full Code

// Function to sanitize and format time
function sanitizeAndFormatTime(time) {
    if (!time) return null;
   
    time = time.trim();
    let matches = time.match(/\d/g);
    if (matches && matches.length === 4) {
        return matches[0] + matches[1] + ":" + matches[2] + matches[3];
    }
    return null;
}

// Function to combine date and time
function combineDateAndTime(date, time) {
    if (!date) return null;

    let dateTime = new Date(date);
    if (time) {
        let hours = parseInt(time.substring(0, 2));
        let minutes = parseInt(time.substring(3, 5));

        dateTime.setHours(hours);
        dateTime.setMinutes(minutes);
    }
    return dateTime;
}

// Main function to populate scheduled start and end date-time
async function populateScheduledStartEndDateTime(formContext) {
    let sessionDate = formContext.getAttribute("tri_sessiondate").getValue();
    let scheduledStartTime = formContext.getAttribute("tri_scheduledstarttime").getValue();
    let scheduledEndTime = formContext.getAttribute("tri_scheduledendtime").getValue();

    let formattedStartTime = sanitizeAndFormatTime(scheduledStartTime);
    let formattedEndTime = sanitizeAndFormatTime(scheduledEndTime);

    let scheduleStartDateTime = combineDateAndTime(sessionDate, formattedStartTime);
    let scheduleEndDateTime = combineDateAndTime(sessionDate, formattedEndTime);

    if (scheduleStartDateTime) {
        formContext.getAttribute("tri_schedulestartdateandtime").setValue(scheduleStartDateTime);
    }
    if (scheduleEndDateTime) {
        formContext.getAttribute("tri_scheduleenddateandtime").setValue(scheduleEndDateTime);
    }
}


Conclusion

This JavaScript implementation in Dynamics 365 demonstrates the power of combining simple coding techniques to achieve a practical business need. By understanding and utilizing regular expressions, string manipulation, and date-time handling in JavaScript, we can effectively manipulate and present data in Dynamics 365 forms.


No comments:

Post a Comment