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

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How to write a JavaScript to Auto Lock a field after end of the day (@11.59PM)

(0) ShareShare
ReportReport
Posted on by

I have a field which is of type Date&Time(Time-Zone Independent), users will be using this field to schedule pickups and now I want to lock this filed once the scheduled day is over i.e at 11:59 PM on the scheduled date. 

We have users in different time zones across United States who will be using this field as an end user. (They can not schedule anything but they should be able to edit this field until 11.59PM of the scheduled date) 

we developed a JavaScript Web Resource for this requirement and its working fine but it is locking the field 7 Hours ahead because of the UTC time conversion formula we have used within the code. 

Any thoughts on how to resolve this issue?  

*This post is locked for comments

I have the same question (0)
  • gdas Profile Picture
    50,091 Moderator on at
    RE: How to write a JavaScript to Auto Lock a field after end of the day (@11.59PM)

    Hi Sunny ,

    Can you try to debug the code and check if its enter into the line where you are making field disabled .

  • Community Member Profile Picture
    on at
    RE: How to write a JavaScript to Auto Lock a field after end of the day (@11.59PM)

    Hi Goutham,

    Thank you so much for your time, I applied the same logic and its working as expected. But is there any way that I can lock that field at 11:59:59(HH:MM:SS), I tried the following but somehow its not working for me

    var currentDay = todaydate.getDate();

                   var todayHr = todaydate.getHours();

                   var todayMin = todaydate.getMinutes();

                   var todaySec = todaydate.getSeconds();

                   var scheduleDay = SheduledDate.getDate();

                   if (currentDay == scheduleDay) {

                       if (todayHr == 23) {

                           if (todayMin == 59) {

                               if (todaySec>=59){

                               Xrm.Page.getControl("new_ScheduleDate").setDisabled(true);

                           }

                       }

                   }

                 }

               }

  • Verified answer
    gdas Profile Picture
    50,091 Moderator on at
    RE: How to write a JavaScript to Auto Lock a field after end of the day (@11.59PM)

    Hi Sunny,

    Try with this , Make sure you need to handle both onload and onsave as well (For OnSave you need to restrict update ) otherwise user will open the form and can be wait few minutes.

            function DateCompare() {
                var dateTimeValue = Xrm.Page.getAttribute("new_ScheduleDate").getValue()
                var SheduledDate = new Date(dateTimeValue);
                // var SheduledDate = new Date("Fri Aug 24 2018 23:59:59 GMT+0530 (India Standard Time)");
                // var todaydate = new Date("Sat Aug 25 2018 23:58:55 GMT+0530 (India Standard Time)");
                var todaydate = new Date();
                var today = todaydate.getFullYear() + '/' + (todaydate.getMonth() + 1) + '/' + todaydate.getDate();
                var Schedule = SheduledDate.getFullYear() + '/' + (SheduledDate.getMonth() + 1) + '/' + SheduledDate.getDate();
                if (today <= Schedule) {
                    var currentDay = todaydate.getDate();
                    var todayHr = todaydate.getHours();
                    var todayMin = todaydate.getMinutes();
                    var scheduleDay = SheduledDate.getDate();
                    if (currentDay == scheduleDay) {
                        if (todayHr == 23) {
                            if (todayMin >= 59) {
                                Xrm.Page.getControl("new_ScheduleDate").setDisabled(true); //Lock field if current date is match and time 23:59
                            }
                        }
                    }
    
                }
                else {
                    Xrm.Page.getControl("new_ScheduleDate").setDisabled(true); // Lock field if schedule date is over
                }
            }

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Aric Levin - MVP Profile Picture

Aric Levin - MVP 2 Moderator

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#3
MA-04060624-0 Profile Picture

MA-04060624-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans