Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

JavaScript with Current Date

Posted on by Microsoft Employee

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

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: JavaScript with Current Date

    Yes I may late to fix the issue ;)

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: JavaScript with Current Date

    Hi,

    This should work , there was two changes you need to do from client side one is you need to add 1 for month and for day you need to use getDate().

    Hope this helps. Please find below updated code- 

            function PrintName() {
                var Vendor = Xrm.Page.getAttribute("new_printvendor").getValue();
                var BRE = Xrm.Page.getAttribute("new_bre").getValue();
                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 (Vendor != null && BRE != null && Invoice != null && Batch != null && Pages != null && Name == null) {
                    var d = new Date();
                    //Commented Code
                    //var PrintMonth = d.getMonth();
                    //var PrintDay = d.getDay();
                    //var PrintYear = d.getFullYear();
                    //New Code
                    var PrintYear = d.getFullYear() ;
                    var PrintMonth = (d.getMonth() + 1);
                    var PrintDay = d.getDate();
                    if (Vendor == 100000001 && BRE == 100000001) {
                        Xrm.Page.getAttribute("new_printfilename").setValue("Multifam" + "_" + "Invoice" + Invoice + "_" + PrintMonth + "." + PrintDay + "." + PrintYear + "_" + Pages + "_" + "No_BRE" + "_" + "Batch" + Batch);
    
                    }
                    if (Vendor == 100000001 && BRE == 100000000) {
                        Xrm.Page.getAttribute("new_printfilename").setValue("Multifam" + "_" + "Invoice" + Invoice + "_" + PrintMonth + "." + PrintDay + "." + PrintYear + "_" + Pages + "_" + "BRE" + "_" + "Batch" + Batch);
    
                    }
    
                    if (Vendor == 100000000 && BRE == 100000001) {
                        Xrm.Page.getAttribute("new_printfilename").setValue("Multifam" + "_" + PrintMonth + "." + PrintDay + "." + PrintYear + "_" + "BillOnly" + "Invoice" + Invoice + "_" + "Batch" + Batch + "_" + Pages);
    
                    }
                    if (Vendor == 100000000 && BRE == 100000000) {
                        Xrm.Page.getAttribute("new_printfilename").setValue("Multifam" + "_" + PrintMonth + "." + PrintDay + "." + PrintYear + "_" + "Invoice" + Invoice + "_" + "Batch" + Batch + "_" + Pages);
    
                    }
                }
    
            }

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: JavaScript with Current Date

    Hello Goutam,

    Thank you, I found a different way to enter it and now it is producing the correct date. I changed it to:

    var d = new Date();

    var PrintMonth = (d.getMonth()+1)+"";

    var PrintDay = d.getDate();

    var PrintYear = d.getFullYear();

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: JavaScript with Current Date

    Hi Goutam,

    Ah thank you, that's probably the problem. It must be pulling the seconds instead of month and day.

    Do you have a suggestion what to enter instead? I want it to produce today's date when the JS is ran and meets the criteria. So if it met the criteria today then it would produce 3.28.2018 for the portion of the print file name it's setting the value for (i.e. Multifam_Invoice69_2.3.2018_25_No_BRE_Batch2).

    Here is the current JS:

    function PrintName()

    {

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

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

    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 (Vendor != null && BRE != null && Invoice != null && Batch != null && Pages != null && Name == null) {

    var d = new Date();

    var PrintMonth = d.getMonth();

    var PrintDay = d.getDay();

    var PrintYear = d.getFullYear();

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

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

     }

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

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

     }

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

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

     }

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

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

     }

     }

    }

  • Verified answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: JavaScript with Current Date

    Hi ,

    No issue , the Date.now() method returns the number of milliseconds elapsed since January 1.

    Please refer below url -

    developer.mozilla.org/.../now

    Hope this helps you.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: JavaScript with Current Date

    Hi Goutam,

    I'm extremely new and not experienced in writing JS, I have just been using Google to figure out how to write it so I do not know how to get the conversion code from integer. How do I get that for you? I don't have a debugger.

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: JavaScript with Current Date

    Could you please share your conversion code from integer , there may be some issue.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: JavaScript with Current Date

    Hello,

    This is js:

    function PrintName()

    {

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

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

    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 (Vendor != null && BRE != null && Invoice != null && Batch != null && Pages != null && Name == null) {

    var d = new Date();

    var PrintMonth = d.getMonth();

    var PrintDay = d.getDay();

    var PrintYear = d.getFullYear();

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

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

     }

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

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

     }

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

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

     }

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

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

     }

    }

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: JavaScript with Current Date

    Hi,

    There should have some conversion issue , please share your code how you are converting integer value to date.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: JavaScript with Current Date

    Yes, attached is a screenshot of the print file name that is produced (I do have it pull the month, day and year from today's date so I can have it entered in a different format.

    Looks a little blurry so I'm not sure if you can read it but it's "2.3.2018"

    print-file-name-produced.png

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,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans