Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM forum
Answered

Short Month and Short Year from Date/Time field using JS

Posted on by 112

Hello experts,

I am going around in circles attempting to create a concatenated column derived from a date/time field.

The code I am using is as follows:

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

function onChange_dov()
{
var objdate = Xrm.Page.getAttribute("rst_asset_dov").getValue();
if (objdate != null)
{
var year = objdate.toLocaleDateString('en', {year: '2-digit'});
var month = objdate.toLocaleDateString('en', {month: '2-digit'});

Xrm.Page.getAttribute("rst_asset_dov_col").setValue((month + "/" + year));
Xrm.Page.getAttribute("rst_asset_dov_col").setSubmitMode('always');
}
}

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

The error I get is telling me the setValue is null:

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

One of the scripts for this record has caused an error. For more details, download the log file.

Cannot read properties of null (reading 'setValue')
Session Id: 070e3b8e-8f6b-47a6-8c47-bedc43eed22c
Correlation Id: 35996f97-9348-4bfc-a4d0-ec1a375d99c6
Event Name: onchange
Web Resource Name: rst_asset_dov_calc
Solution Name: ActivePublisher Name: DefaultPublisherSCFMCRM
Time: Fri Feb 11 2022 09:56:50 GMT+0800 (Australian Western Standard Time)
Download Log File
OK
-------------------------------------------------------

Where I test the code and variables and output using objdate = new Date();, there is no issue, it presents 02/22 as I would expect.

Any suggestions would be greatly appreciated.

Thank you in advance,

Jayden.

  • JaydenPerez17 Profile Picture
    JaydenPerez17 112 on at
    RE: Short Month and Short Year from Date/Time field using JS

    Disregard Guido,

    I have answered my own question.

    Thanks again for your help.

    Jayden.

  • JaydenPerez17 Profile Picture
    JaydenPerez17 112 on at
    RE: Short Month and Short Year from Date/Time field using JS

    Hi Guido,

    That is spot on, it ultimately required the field being present on the form in order to apply.

    I dont necessarily want this field to be visible, can it be hidden and still usable?

    Thank you for your assistance,

    Jayden.

  • Verified answer
    Guido Preite Profile Picture
    Guido Preite 54,065 Moderator on at
    RE: Short Month and Short Year from Date/Time field using JS

    the error message you are receiving usually means the field is not on the form (in your case the one called rst_asset_dov_col)

    I would double check if the field is present on the form first

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

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,558 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,645 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans