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)

Javascript to Retrieve/Set Field Values on Load

(0) ShareShare
ReportReport
Posted on by

Hi All,


I'm running into an issue and I can't seem to find a solution.  I am attempting to create some Javascript that will run on form load, compare two dates, and populate an option set, but everything I try spits out:

I've tried multiple different ways to write the script, and below is how it currently sits.  My Javascript is a little rusty, and any help that anybody can offer would be much appreciated.

Version:  On-Premise 2013


Activity: 

Compare date in a field against today's date, and populate an option set with 'Yes' (value 100000000) if it is within 4 days.

Javascript:

function CalcDaysDifference()

{

var today = new Date();

var lastAction = Xrm.Page.getAttribute("LastActionDate").getValue();

if(lastAction != NULL)

{

if (today - lastAction <= 4)

{

xrm.page.getAttribute("LastActionWithin4Days").setValue(100000000);

}

}

}

*This post is locked for comments

I have the same question (0)
  • ansrikanth Profile Picture
    3,115 on at

    Hi 

    What is the issue you are facing. The general practice is first get the Attribute (Xrm.Page.getAttribute("LastActionDate")) and ensure it is not null & not undefined and then use .getValue() of it.

    And the if condition should be if((today-lastaction)/(1000*60*60*24)<=4) ==> Javascript date difference with simple Minus (-) will return in Milli seconds, so you have to convert that in to dates. Probably thats why your code always showing condition passed.

    Note: You might need to convert your "lastAction" to new Date before you start the if conditon.

    Happy coding :)

    -Srikanth

  • Verified answer
    Mahadeo Matre Profile Picture
    17,021 on at

    var LastActionDate = Xrm.Page.getAttribute('LastActionDate').getValue();

        var millisecondsPerDay = 1000 * 60 * 60 * 24;

        var millisBetween = LastActionDate.getTime() - today.getTime();

        var NoOfdays = millisBetween / millisecondsPerDay;

        if (NoOfdays < 4)

    xrm.page.getAttribute("LastActionWithin4Days").setValue(100000000);

    Try this code

  • Community Member Profile Picture
    on at

    Thank you Mahadeo.  That worked perfectly.

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!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Varsha deshpande Profile Picture

Varsha deshpande 5

#2
JS-09031509-0 Profile Picture

JS-09031509-0 3

#3
Ciprian  P Profile Picture

Ciprian P 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans