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)

Retrieve the day of a pickup based upon date field

(0) ShareShare
ReportReport
Posted on by

Hi, 

I am trying to bring back the day of a pickup using the date field.  I.e 10/02/15 is 'Tuesday'.

So far i've created a JS web resource called dayofpickup with the code (i realise its not complete yet but i want to show the date in that field before I grab the day.

function GetDate("sourcename")
{
return Xrm.Page.data.entity.attributes.get("sourcename").getValue();
}

Added it to the pickup form as a new field called Day.

Added the JS to the onload event on the form properties but date is not flowing through to the field.

Can anyone provide advice on why the field may not be currently populating with the date and how I can adjust my JScript to show the day of pickup?

Thanks Jon

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Mithilesh Kumar Profile Picture
    10,047 on at

    Hi Jon,

    If your "sourcename" is a Date and Time field it will return values something like this, which reflects the users time zone set under personal options.

    8547.1.jpg

    Once you get the Date Time value you will be able to extract the Weekday from the value. However, the getDay() function will be return value as integer starting from Sunday (being 0) - Saturday (being 6).

    Example from above value

    Hope that helps

    Thanks

    Please mark my post as verified if you found it helpful.

  • Community Member Profile Picture
    on at

    Thanks for the response Mithilesh,

    So if the Java script code is on the onload event on the form properties, how do I  link the value returned to the 'day' field I created?

    The only event trigger is onchange and since no change is happening I cannot work out how the field is to be populated with the value returned from the code.

    I'm sure I am missing something simple.

    cheers,

    Jon

  • Suggested answer
    Mithilesh Kumar Profile Picture
    10,047 on at

    Hi Jon,

    If you want the JScript function to be on the Onload of the Form, you need to apply this logic

    1. Grab the Date Time field value

    2. Get the Day. This will return an integer (0-6). Zero for Sunday and so on.

    3. You will have to use a Switch Case statement in your code to link the Day

    4. Populate the Text field with the Value

    Here is a simple code,

    function GetDay(){
     var Date = Xrm.Page.data.entity.attributes.get("createdon").getValue();
     var intDay = Date.getDay();
     var weekDay;

     switch(intDay) {
          case 0:
              weekDay = "Sunday";
              break;

      case 1:
              weekDay = "Monday";
              break;

      case 2:
              weekDay = "Tuesday";
              break;

      case 3:
              weekDay = "Wednesday";
              break;

      case 4:
              weekDay = "Thursday";
              break;

      case 5:
              weekDay = "Friday";
              break;

      case 6:
              weekDay = "Saturday";
              break;
     }
     Xrm.Page.data.entity.attributes.get("description").setValue(weekDay);
    }

    Output:

    Note: Above code will not work for new record as created date is not available then. In that case you can use the On Save instead of Onload

     

    Hope that helps

    Thanks

    Please mark my post as verified if you found it helpful

  • Verified answer
    Guido Preite Profile Picture
    54,086 Moderator on at

    Mithilesh is correct, but in this case an array is simpler to use.

    In addition is not a best practice to call a variable as a standard JavaScript object (as Date);

    function GetDay(){
    var weekDays = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
    // createdon instead of Date
    var createdon = Xrm.Page.data.entity.attributes.get("createdon").getValue();
    var weekDay = weekDays[createdon.getDay()];
    Xrm.Page.data.entity.attributes.get("description").setValue(weekDay);
    }
  • Mithilesh Kumar Profile Picture
    10,047 on at

    Hi Jon,

    Guido is correct, if you only need Week Day you can go with array other that switch case statement. Also do take care about the Variable name, never use Date as it is reserved keyword. I missed out "obj" prefix.

    var objDate = Xrm.Page.data.entity.attributes.get("createdon").getValue();

    However, if you have other block of codes to execute based on Week Day you can use the Switch Case statement.

    Thanks

  • Suggested answer
    Sreeekanth Profile Picture
    95 on at

    hiii

    my Doubt is "Createdon" field will get the date and time on On-save of the form.

    Then how can i get day from createdon and to set that date value to the my custom field on form.

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