Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Javascript to Retrieve/Set Field Values on Load

Posted on by Microsoft Employee

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

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Javascript to Retrieve/Set Field Values on Load

    Thank you Mahadeo.  That worked perfectly.

  • Verified answer
    Mahadeo Matre Profile Picture
    Mahadeo Matre 17,021 on at
    RE: Javascript to Retrieve/Set Field Values on Load

    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

  • ansrikanth Profile Picture
    ansrikanth 3,115 on at
    RE: Javascript to Retrieve/Set Field Values on Load

    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

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans