locked
Error in Code RRS feed

  • Question

  • // JavaScript source code

    function accnumbergeneration() {

        var accname = Xrm.Page.getControl("new_firstname");

     alert("hi");

        if (accname != null) {

            accname = accname.getValue();

            accname = accname.substring(0, 2);

         }

        else

            accname = "";

        var today = new Date().toString();

        var mon = today.substring(4, 7);

        var day = today.substring(8, 10);

        var year = today.substring(11, 15);

        if (mon == "Jan")

            mon = "01";

        else if (mon == "Feb")

            mon = "02";

        else if (mon == "Mar")

            mon = "03";

        else if (mon == "Apr")

            mon = "04";

        else if (mon == "May")

            mon = "05";

        else if (mon == "Jun")

            mon = "06";

        else if (mon == "Jul")

            mon = "07";

        else if (mon == "Aug")

            mon = "08";

        else if (mon == "Sep")

            mon = "09";

        else if (mon == "Oct")

            mon = "10";

        else if (mon == "Nov")

            mon = "11";

        else if (mon == "Dec")

            mon = "12";

        Xrm.Page.getAttribute("new_accountnumber").setValue(accname.toUpperCase() + "" + day + "" + mon + "" + year);

    }

    Tuesday, September 30, 2014 4:46 AM

Answers

  • Solved... just  replace this below code like

      var accname = Xrm.Page.getControl("new_firstname");

    to, like..

     Var accname=Xrm.Page.getAttribute("new_firstname");

    Tuesday, September 30, 2014 5:13 AM

All replies

  • Hi Krishna,

    Use getValue() in the if condition to check null.

      if (accname.getValue != null) 

    Regards,

    Saad


    Tuesday, September 30, 2014 4:55 AM
  • you are using getControl but you need to use getAttribute to get the field value

    and for the generation of the date is not necessary that amount of if conditions. try with:

    function accnumbergeneration() {
    var accname = Xrm.Page.getAttribute("new_firstname").getValue();
    if (accname != null) {
        accname = accname.substring(0, 2);
    } else {
        accname = "";
    }
    var today = new Date();
    var year = today.getFullYear();
    var month = today.getMonth() + 1;
    var day = today.getDate();
    day = day < 10 ? '0' + day : day;
    month = month < 10 ? '0' + month : month;
    Xrm.Page.getAttribute("new_accountnumber").setValue(accname.toUpperCase() + "" + day + "" + month + "" + year);
    }


    My blog: www.crmanswers.net - Rockstar 365 Profile

    Tuesday, September 30, 2014 5:08 AM
  • Solved... just  replace this below code like

      var accname = Xrm.Page.getControl("new_firstname");

    to, like..

     Var accname=Xrm.Page.getAttribute("new_firstname");

    Tuesday, September 30, 2014 5:13 AM