Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Getting error

(0) ShareShare
ReportReport
Posted on by 95

Business Scenario :

                              I want to get day from createdon field.

                              But createdon filed get date and time when we save the form.

                              please help me how to get day from createdon field using java script.

I wrote JS LIKE below

function GetDay(){

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

var intDay = objDate.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("new_createdday").setValue(weekDay);

}

Iam getting error like below

0407.ERROR1.png

Reply

*This post is locked for comments

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Getting error

    Hi Sreeekanth,

    Did a quick test on this. This script won't work for the new records as the createdon field will be blank/ null before the new records.

    You can use the updated version which will set the today's date from JavaScript if the form is create.

    Please also note that as we are getting today's date from JavaScript, it will return the date from the computer it is executing i.e. if you change your computers date then it will return day for that date.

    ---------------------------------

    function GetDay() {

       // Initialize with todays date

       var objDate = new Date();

       // Check the form type if its create

       if (Xrm.Page.ui.getFormType() != 1) {

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

       }

       var intDay = objDate.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;

       }

       alert(weekDay);

       Xrm.Page.data.entity.attributes.get("new_createdday").setValue(weekDay);

    }

    -------------------------------------

  • Sreeekanth Profile Picture
    Sreeekanth 95 on at
    RE: Getting error

    i tried got the same error

    1651.ERROR2.png

  • RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Getting error

    Try also putting it on the form, its currently on the form.

    Also, test your script on existing record which already has createdon value to isolate the issue.

  • Sreeekanth Profile Picture
    Sreeekanth 95 on at
    RE: Getting error

    5282.ERROR1.png

  • Sreeekanth Profile Picture
    Sreeekanth 95 on at
    RE: Getting error

    hii

    i added the field on form.

    but The Createdon field getting value after saving the form.

    i also written JS onsave

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Getting error

    Hi Sreekanth,

    The above error is because objDay is null. This could be because you have not added the createdon field on the 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

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,458 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans