web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

JavaScript with Current Date

(0) ShareShare
ReportReport
Posted on by

I am trying to enter data into a current field based on the selection of other fields. It was all working until I tried to enter in today's date. Now it is filling in the field before checking the conditions and for the date it says undefined. I get "Multifam_69_undefined.undefined.undefined_250_BRE_3" using the javascript below. All is correct other than where it should be the date and says undefined. Do you know where I am going wrong?

function PrintName()
{
var Vendor = Xrm.Page.getAttribute("new_printvendor").getValue();
var BRE = Xrm.Page.getAttribute("new_bre").getText();
var Invoice = Xrm.Page.getAttribute("new_invoicetemplate").getText();
var Batch = Xrm.Page.getAttribute("new_printbatch").getValue();
var Pages = Xrm.Page.getAttribute("new_printfileofpages").getValue();
var Name = Xrm.Page.getAttribute("new_printfilename").getValue();
if (BRE != null && Invoice != null && Batch != null && Pages != null && Name == null) {
var d = Date.now();
var PrintMonth = d.getMonth();
var PrintDay = d.getDay();
var PrintYear = d.getFullYear();
}
if (Vendor == 100000001) {
Xrm.Page.getAttribute("new_printfilename").setValue( "Multifam" + "_" + Invoice + "_" + PrintMonth + "." + PrintDay + "." + PrintYear + "_" + Pages + "_" + BRE + "_" + Batch);
}
if (Vendor == 100000000) {
Xrm.Page.getAttribute("new_printfilename").setValue( "Multifam" + "_" + PrintMonth + "." + PrintDay + "." + PrintYear + "_" + "BillOnly" + Invoice + "_" + Batch + "_" + Pages);
}
if (BRE == 100000000 && Vendor == 100000000) {
Xrm.Page.getAttribute("new_printfilename").setValue( "Multifam" + "_" + PrintMonth + "." + PrintDay + "." + PrintYear + "_" + Invoice + "_" + Batch + "_" + Pages);
}
}

*This post is locked for comments

I have the same question (0)
  • Mahendar Pal Profile Picture
    45,095 on at

    Hi,

    Just curious, any reason for using Date.now(), instead of simply

    var d=new Date(); //which will return current date and time object

  • Community Member Profile Picture
    on at

    I tried var d = new Date(); and I'm still getting an error. I've been trying all kinds of ways!

  • Suggested answer
    Mahendar Pal Profile Picture
    45,095 on at

    your assignment code should come under if so it should be like below

    function PrintName()

    {

    var Vendor = Xrm.Page.getAttribute("new_printvendor").getValue();

    var BRE = Xrm.Page.getAttribute("new_bre").getText();

    var Invoice = Xrm.Page.getAttribute("new_invoicetemplate").getText();

    var Batch = Xrm.Page.getAttribute("new_printbatch").getValue();

    var Pages = Xrm.Page.getAttribute("new_printfileofpages").getValue();

    var Name = Xrm.Page.getAttribute("new_printfilename").getValue();

    if (BRE != null && Invoice != null && Batch != null && Pages != null && Name == null) {

    var d = Date.now();

    var PrintMonth = d.getMonth();

    var PrintDay = d.getDay();

    var PrintYear = d.getFullYear();

    if (Vendor == 100000001) {

    Xrm.Page.getAttribute("new_printfilename").setValue( "Multifam" + "_" + Invoice + "_" + PrintMonth + "." + PrintDay + "." + PrintYear + "_" + Pages + "_" + BRE + "_" + Batch);

    }

    if (Vendor == 100000000) {

    Xrm.Page.getAttribute("new_printfilename").setValue( "Multifam" + "_" + PrintMonth + "." + PrintDay + "." + PrintYear + "_" + "BillOnly" + Invoice + "_" + Batch + "_" + Pages);

    }

    if (BRE == 100000000 && Vendor == 100000000) {

    Xrm.Page.getAttribute("new_printfilename").setValue( "Multifam" + "_" + PrintMonth + "." + PrintDay + "." + PrintYear + "_" + Invoice + "_" + Batch + "_" + Pages);

    }

    }

    }

  • Community Member Profile Picture
    on at

    Do I have to use something like var d = setValue.new Date(); or var d = getValue.new date();?

  • Community Member Profile Picture
    on at

    Sorry submitted the question regarding setValue and getValue before I saw your response, I will try this, thanks.

  • Verified answer
    Mahendar Pal Profile Picture
    45,095 on at

    You can simply use var d=new Date();

    var PrintMonth = d.getMonth();

    var PrintDay = d.getDay();

    var PrintYear = d.getFullYear();

  • Community Member Profile Picture
    on at

    Hmm I'm still getting an error TypeError: d.getMonth is not a function at PrintName. I'm checking to make sure I have all the fields labeled correctly.

    function PrintName()

    {

    var Vendor = Xrm.Page.getAttribute("new_printvendor").getValue();

    var BRE = Xrm.Page.getAttribute("new_bre").getText();

    var Invoice = Xrm.Page.getAttribute("new_invoicetemplate").getText();

    var Batch = Xrm.Page.getAttribute("new_printbatch").getValue();

    var Pages = Xrm.Page.getAttribute("new_printfileofpages").getValue();

    var Name = Xrm.Page.getAttribute("new_printfilename").getValue();

    if (BRE != null && Invoice != null && Batch != null && Pages != null && Name == null) {

    var d = Date.now();

    var PrintMonth = d.getMonth();

    var PrintDay = d.getDay();

    var PrintYear = d.getFullYear();

    if (Vendor == 100000001) {

    Xrm.Page.getAttribute("new_printfilename").setValue( "Multifam" + "_" + Invoice + "_" + PrintMonth + "." + PrintDay + "." + PrintYear + "_" + Pages + "_" + BRE + "_" + Batch);

     }

    if (Vendor == 100000000 && BRE == 100000001) {

    Xrm.Page.getAttribute("new_printfilename").setValue( "Multifam" + "_" + PrintMonth + "." + PrintDay + "." + PrintYear + "_" + "BillOnly" + Invoice + "_" + Batch + "_" + Pages);

     }

    if (BRE == 100000000 && Vendor == 100000000) {

    Xrm.Page.getAttribute("new_printfilename").setValue( "Multifam" + "_" + PrintMonth + "." + PrintDay + "." + PrintYear + "_" + Invoice + "_" + Batch + "_" + Pages);

     }

     }

    }

  • Community Member Profile Picture
    on at

    I changed it to var d=new Date(); and it finally worked! Thank you.

  • Verified answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi ,

    var d = Date.now(); will not work please replace with below.

    var d = new Date();

    This will also return current date time.

    Hope this helps

  • Community Member Profile Picture
    on at

    Thank you, that did make it work.

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
JS-09031509-0 Profile Picture

JS-09031509-0 3

#2
AS-17030037-0 Profile Picture

AS-17030037-0 2

#2
Mark Eckert Profile Picture

Mark Eckert 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans