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 :
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

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

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans