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 :
Microsoft Dynamics CRM (Archived)

Script not executing required functionality

(0) ShareShare
ReportReport
Posted on by

Hi experts, 

I am using scripting to on a field called "new_timer" that acts as a timer triggered when any field on the record changes. I use getIsDirty Method, but the timer is still starting as soon as I open some record and not when a field changes. 

//Initialize time attributes
var seconds = 0, minutes = 0, hours = 0,t;
var myVar = setInterval(add, 2000);
function add() 
{
	
    var oppAttributes = Xrm.Page.data.entity.attributes.get();
    if (oppAttributes != null) 
    {
        for (var i in oppAttributes) 
        {
               if (oppAttributes[i].getIsDirty()) 
               {
						seconds++;
					    if (seconds >= 60) 
					    {
					        seconds = 0;
					        minutes++;
					        if (minutes >= 60) 
					        {
					            minutes = 0;
					            hours++;
					        }
				    	}
				    	clearTimeout(myVar);

               }

           

        }
    }


    
	//creating time value in hh:mm:ss formate
    var tmp = (hours ? (hours > 9 ? hours : "0" + hours) : "00") + ":" + (minutes ? (minutes > 9 ? minutes : "0" + minutes) : "00") + ":" + (seconds > 9 ? seconds : "0" + seconds);
	Xrm.Page.getAttribute("new_timer").setValue(tmp);

	//recursively calling timer
	timer();
    
}

//main timer method
function timer() {
    t = setTimeout(add, 1000);
}





Is there anything missing in my code that I should add?

Thanks,

Jon

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hello Jon,

    This is good experience for me like this type of requirement. Thanks for sharing .

    Seems  time is not clearing using clearTimeout.

    Please try to write  window.clearInterval(myVar);  instead of   clearTimeout(myVar);

    Hope this helps

  • Suggested answer
    Shahbaaz Ansari Profile Picture
    6,211 on at

    Hi Jon,

    you just want to catch the time when the first field value change or when ever any field change you want to update your new timer value?

    Thanks,

    Shahbaaz

  • Suggested answer
    Shahbaaz Ansari Profile Picture
    6,211 on at

    and also you are clearing timeout in if condition clearTimeout(myVar);

    and below you are again setting timer()

    Please describe your requirement more clearly so we can help.

    Thanks,

    Shahbaaz

  • Community Member Profile Picture
    on at

    Hi all,

    So to be more specific, when we open a record and "change" any field (except the new_timer field) on the form, I want my function to be triggered and start the timer continuously.

    Then when the user clicks on the save button on the bottom right side of the page, the time is captured in a custom activity which is irrelevant for this specific requirement.

    Thanks,

    Jon

  • Community Member Profile Picture
    on at

    All I need is for the web resource to be triggered when any field (except new_timer) is changed.

  • Suggested answer
    Shahbaaz Ansari Profile Picture
    6,211 on at

    Hi Jon,

    One last question, i understand you want to capture the time in your field,

    lets say the field changed at 08/03/2018 11:20 AM and User saved the form at 08/03/2018 11:25 AM

    so you want to capture the difference or you want to capture the save time???

    I guess you want the difference!!! m i right?

    Thanks,

    Shahbaaz

  • Verified answer
    Shahbaaz Ansari Profile Picture
    6,211 on at

    Hi Jon,

    I have made few changes in my code, now i have check that form is dirty or not, if form is dirty means any field on the form has changed. I have capture that time as "FiledChangeTime" and also i have set the flag in that "firstfieldchanged" to 1, now after few second, minute or hour user will save the form, at that time, is dirty property of from will be false, so in this case we will calculate the time difference and set in the field.

    We don't need to clear the interval, i have tested the below code, just don't change any thing, keep the code and on the form load event add "timer" function.

    var firstfieldchanged = 0;
    var FiledChangeTime = new Date();
    var SaveTime = new Date();
    function add()
    {
    var isFormDirty = Xrm.Page.data.entity.getIsDirty();

    if(isFormDirty==true && firstfieldchanged==0)
    {
    // when the field changed for the first time
    FiledChangeTime = new Date();
    firstfieldchanged = 1;
    }
    else if(isFormDirty==false && firstfieldchanged==1)
    {

    firstfieldchanged = 0;
    SaveTime = new Date();
    var seconds = (SaveTime.getTime() - FiledChangeTime.getTime()) / 1000;
    var timer = secondsToHms(seconds);
    //creating time value in hh:mm:ss formate
    Xrm.Page.getAttribute("new_timer").setValue(timer);
    Xrm.Page.data.entity.save();
    }
    }

    function secondsToHms(d) {
    var d = Number(d);
    var h = Math.floor(d / 3600);
    var m = Math.floor(d % 3600 / 60);
    var s = Math.floor(d % 3600 % 60);

    return h.toString() +":"+ m.toString() +":"+ s.toString();
    }

    //main timer method
    function timer() {
    var myVar = setInterval(add, 10000);
    }

    See below screen shot

    0572.Timer.PNG

    If it helps you, mark my answer as verified and if it did not than let me know the issue.

    Best Regards,

    Shahbaaz

  • Community Member Profile Picture
    on at

    Hi Shahbaaz,

    Correct, I need it to capture the difference between any field change and form save.

    However the client needs to see an active stopwatch every time the form is loaded.

    So for example I open the form today, the timer value difference is 00:11:34 is saved as a phone call activity.

    Now, when I open the form again, the timer should again start.

  • Suggested answer
    Shahbaaz Ansari Profile Picture
    6,211 on at

    Hi Jon,

    Did you tried above code?

    Thanks,

    Shahbaaz

  • Community Member Profile Picture
    on at

    Hi Shahbaaz,

    Yes I have tried the code, but its not working.

    Thanks,

    Jon

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
JS-09031509-0 Profile Picture

JS-09031509-0 3

#2
AS-17030037-0 Profile Picture

AS-17030037-0 2

#2
Mark Eckert Profile Picture

Mark Eckert 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans