locked
Add week and year to opportunity RRS feed

  • Question

  • Hi All,

    My client would like two fields adding to the opportunities form, one for the week the opportunity was created and the other for the year the opportunity was created. They should be locked and automatically filled in with the relevant information and not change once set.

    Is this possible to do and if so could someone please give me some pointers?

    Thanks


    David Hodgson
    Add me on Twitter and LinkedIn
    www.infinitygroup.co.uk

    Sunday, June 16, 2013 4:08 PM

Answers

  • The code is ok, but I just checked and if you set the value to a text field (as my suggestion) is necessary to make the value as string adding an empty string ('')
    if the target field is numeric the previous code is ok.

    function Form_OnLoad() {
        SetWeekYear();
    }
    
    function SetWeekYear() {
    if (Xrm.Page.ui.getFormType()==1)
    {
       var today = new Date();
       var year = today.getFullYear();
    
       var onejan = new Date(year,0,1);
       var week = Math.ceil((((today - onejan) / 86400000) + onejan.getDay()+1)/7);
    
       Xrm.Page.getAttribute("new_year").setValue(year +'');
       Xrm.Page.getAttribute("new_week").setValue(week +'');
    }
    }


    My blog: www.crmanswers.net



    Sunday, June 16, 2013 5:35 PM

All replies

  • Hi David,
    just create the two fields (I suggest text type, because you will not see the delimiter for the year like 2,013)
    This is the javascript to get the current week and the current year, put this inside the onload event.

    if (Xrm.Page.ui.getFormType()==1)
    {
       var today = new Date();
       var year = today.getFullYear();
    
       var onejan = new Date(year,0,1);
       var week = Math.ceil((((today - onejan) / 86400000) + onejan.getDay()+1)/7);
    
       Xrm.Page.getAttribute("new_year").setValue(year);
       Xrm.Page.getAttribute("new_week").setValue(week);
    }

    note: I took the week algorithm from here: http://stackoverflow.com/questions/7765767/show-week-number-with-javascript


    My blog: www.crmanswers.net

    Sunday, June 16, 2013 4:28 PM
  • Thanks again, could you cast your eye over this for me, hopefully I am learning and have this right.
    function Form_OnLoad() {
        SetWeekYear();
    }
    
    function SetWeekYear() {
    if (Xrm.Page.ui.getFormType()==1)
    {
       var today = new Date();
       var year = today.getFullYear();
    
       var onejan = new Date(year,0,1);
       var week = Math.ceil((((today - onejan) / 86400000) + onejan.getDay()+1)/7);
    
       Xrm.Page.getAttribute("new_year").setValue(year);
       Xrm.Page.getAttribute("new_week").setValue(week);
    }
    }


    David Hodgson
    Add me on Twitter and LinkedIn
    www.infinitygroup.co.uk

    Sunday, June 16, 2013 5:28 PM
  • The code is ok, but I just checked and if you set the value to a text field (as my suggestion) is necessary to make the value as string adding an empty string ('')
    if the target field is numeric the previous code is ok.

    function Form_OnLoad() {
        SetWeekYear();
    }
    
    function SetWeekYear() {
    if (Xrm.Page.ui.getFormType()==1)
    {
       var today = new Date();
       var year = today.getFullYear();
    
       var onejan = new Date(year,0,1);
       var week = Math.ceil((((today - onejan) / 86400000) + onejan.getDay()+1)/7);
    
       Xrm.Page.getAttribute("new_year").setValue(year +'');
       Xrm.Page.getAttribute("new_week").setValue(week +'');
    }
    }


    My blog: www.crmanswers.net



    Sunday, June 16, 2013 5:35 PM
  • That works.

    What if I have these fields set as read-only, will I need to adjust the script?

    Thanks


    David Hodgson
    Add me on Twitter and LinkedIn
    www.infinitygroup.co.uk

    Sunday, June 16, 2013 5:47 PM
  • if the fields are read-only you need to specify the setSubmitMode, in this way the values will be saved

    // ... previous code Xrm.Page.getAttribute("new_year").setValue(year +''); Xrm.Page.getAttribute("new_week").setValue(week +''); Xrm.Page.getAttribute("new_year").setSubmitMode("always"); Xrm.Page.getAttribute("new_week").setSubmitMode("always");
    // ... rest of the code


    My blog: www.crmanswers.net

    Sunday, June 16, 2013 5:57 PM
  • Thanks Guido, I think I will use the same code as our other post to make the field read only when the form is saved. We don't want these fields to change once they have been populated.


    David Hodgson
    Add me on Twitter and LinkedIn
    www.infinitygroup.co.uk

    Sunday, June 16, 2013 6:01 PM
  • Hi Guido,

    When I use this code along with the code to set two fields read-only the week and year are entered but the fields do not set to read-only.

    Both bits of code are set as Form_OnLoad events however I suspect that the Week and Year script makes the form open in create mode?

    Can you shed any light?


    David Hodgson
    Add me on Twitter and LinkedIn
    www.infinitygroup.co.uk

    Sunday, June 16, 2013 7:48 PM
  • Hi David,
    the two fields must be set as always read-only by changing their properties in the form.

    in this way when you set the setSubmitMode to always the value is saved.


    My blog: www.crmanswers.net

    Sunday, June 16, 2013 7:59 PM
  • Apologies, I must have worded this incorrectly.

    I am using the following two jscripts on the Opportunities form...

    function Form_OnLoad() {
        LockSellingField();
    }
    
    function LockSellingField() {
    // get the field
    var nameControl = Xrm.Page.getControl("new_sellingprice");
    var nameControl1 = Xrm.Page.getControl("new_grossprofit");
    // check if we are not in Create Mode
    if (Xrm.Page.ui.getFormType()!=1) {
       // we disable the field
       nameControl.setDisabled(true);
       nameControl1.setDisabled(true);
    }
    }

    and

    function Form_OnLoad() { SetWeekYear(); } function SetWeekYear() { if (Xrm.Page.ui.getFormType()==1) { var today = new Date(); var year = today.getFullYear(); var onejan = new Date(year,0,1); var week = Math.ceil((((today - onejan) / 86400000) + onejan.getDay()+1)/7); Xrm.Page.getAttribute("new_year").setValue(year +''); Xrm.Page.getAttribute("new_week").setValue(week +''); Xrm.Page.getAttribute("new_year").setSubmitMode("always"); Xrm.Page.getAttribute("new_week").setSubmitMode("always"); }

    )

    When both are active the locksellingfield script does not work, the week and year are filled in.

    Any ideas?


    David Hodgson
    Add me on Twitter and LinkedIn
    www.infinitygroup.co.uk

    Sunday, June 16, 2013 8:23 PM
  • you need only one Form_OnLoad function, just put the three functions inside the same webresource and change the Form_OnLoad function in this way:

    function Form_OnLoad() {
        LockSellingField();
        SetWeekYear();
    }


    My blog: www.crmanswers.net

    Sunday, June 16, 2013 8:29 PM
  • Heres the new code, but it does not seem to work...

    function Form_OnLoad() {
        LockSellingField();
        SetWeekYear();
    }
    
    function LockSellingField() {
    // get the field
    var nameControl = Xrm.Page.getControl("new_sellingprice");
    var nameControl1 = Xrm.Page.getControl("new_grossprofit");
    // check if we are not in Create Mode
    if (Xrm.Page.ui.getFormType()!=1) {
       // we disable the field
       nameControl.setDisabled(true);
       nameControl1.setDisabled(true);
    }
    
    function SetWeekYear() {
    if (Xrm.Page.ui.getFormType()==1)
    {
       var today = new Date();
       var year = today.getFullYear();
    
       var onejan = new Date(year,0,1);
       var week = Math.ceil((((today - onejan) / 86400000) + onejan.getDay()+1)/7);
    
       Xrm.Page.getAttribute("new_year").setValue(year +'');
       Xrm.Page.getAttribute("new_week").setValue(week +'');
       Xrm.Page.getAttribute("new_year").setSubmitMode("always");
       Xrm.Page.getAttribute("new_week").setSubmitMode("always");
    }
    }


    David Hodgson
    Add me on Twitter and LinkedIn
    www.infinitygroup.co.uk

    Sunday, June 16, 2013 9:20 PM
  • Hi David, you are missing the closing brace for the LockSellingField

    function Form_OnLoad() {
        LockSellingField();
        SetWeekYear();
    }
    
    function LockSellingField() {
    // get the field
    var nameControl = Xrm.Page.getControl("new_sellingprice");
    var nameControl1 = Xrm.Page.getControl("new_grossprofit");
    // check if we are not in Create Mode
    if (Xrm.Page.ui.getFormType()!=1) {
       // we disable the field
       nameControl.setDisabled(true);
       nameControl1.setDisabled(true);
    }
    } // this one was missing
    
    function SetWeekYear() {
    if (Xrm.Page.ui.getFormType()==1)
    {
       var today = new Date();
       var year = today.getFullYear();
    
       var onejan = new Date(year,0,1);
       var week = Math.ceil((((today - onejan) / 86400000) + onejan.getDay()+1)/7);
    
       Xrm.Page.getAttribute("new_year").setValue(year +'');
       Xrm.Page.getAttribute("new_week").setValue(week +'');
       Xrm.Page.getAttribute("new_year").setSubmitMode("always");
       Xrm.Page.getAttribute("new_week").setSubmitMode("always");
    }
    }


    My blog: www.crmanswers.net

    Sunday, June 16, 2013 9:23 PM
  • WOW those braces...I might get the hang of them one day!

    David Hodgson
    Add me on Twitter and LinkedIn
    www.infinitygroup.co.uk

    Sunday, June 16, 2013 9:26 PM