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 :
Customer experience | Sales, Customer Insights,...
Suggested Answer

Javascript track when field loses focus

(0) ShareShare
ReportReport
Posted on by 53

We have a business need to check if users are filling out time entry details in current time or if they are going back and updating after the time entry should be closed.  On our time entry entity we have 4 fields actual start, actual end, time entry details and real time (yes/no).  If focus is lost outside of the start and end time meaning if the user edits time entry details between those times (or if end time is null) then the time entry is considered real time if user edits the time entry details outside of the actual start and actual end times the time entry is considered not real time. I am pretty new to javascript so any help is very much appreciated!!  Also not entirely sure if this should solely be on the field as an OnChange event or on the form as an OnLoad event or both.  Thanks again!

I have this java script but it doesnt appear to be tracking the blur event correctly:

function RealTime(executionContext) {
  var formContext = executionContext.getFormContext();
  var actualStart = function monitorFieldFocus() {
  var actualStart = formContext.getAttribute("e3_actualstart").getValue();
  var actualEnd = formContext.getAttribute("e3_actualend").getValue();
  var realTime = formContext.getAttribute("e3_realtime");

  var startTime = new Date(actualStart);
  var endTime = new Date(actualEnd);

  formContext.getControl("e3_timeentrydetails").addOnBlur(function () {
    var fieldValue = formContext.getAttribute("e3_timeentrydetails").getValue();
    var fieldTime = new Date(fieldValue);

    if (fieldTime < startTime || fieldTime > endTime) {
      realTime.setValue(false);
    }
  })}}


I have the same question (0)
  • Suggested answer
    XM-22040801-0 Profile Picture
    11 on at

    Hi,

    What I understood:

    When the user modifies the field e3_timeentrydetails, determine if it is a real time modification or not. Real time means that the change was made between the start and end date.

    In javascript this would give a event handler that trigger on e3_timeentrydetails changes.
    The field change event is triggered when the field has lost focus (click on another field, just before save the record, etc.).

    Below is an event handler (called onTimeEntryDetailsChange) that modified the e3_realtime field accordingly:

    function onTimeEntryDetailsChange(execCtx) {
        const formCtx = execCtx.getFormContext();
        const startAttr = formCtx.getAttribute("e3_actualstart");
        const endAttr = formCtx.getAttribute("e3_actualend");
        const realTimeAttr = formctx.getAttribute("e3_realtime");
        
        const startDate = startAttr.getValue();
        const endDate = endAttr.getValue();
        const currentDate = new Date();
        
        // Modified between time entry dates ?
        const modifiedBetweenDate = (startDate != null && endDate != null && startDate < currentDate && endDate > currentDate);
        
        realTimeAttr.setValue(modifiedBetweenDate);
    }

    This event handler must be registered to the on field change event of the e3_timeentrydetails field. Don't forget to check the "Pass execution context as first parameter" box when you register it.

    This behavior can also be done via a real-time workflow, Power Automate or a plugin. These tools have the advantage of not being executed in the browser but on the server side, which means that the behavior would also be triggered if the field is modified by an external application (via API), an Excel import, etc. Let me know if you need it.

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 > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 137 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 57

#3
Jimmy Passeti Profile Picture

Jimmy Passeti 50 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans