locked
MSCRM Can i set this base on current date and field By using Javascript??? RRS feed

  • Question

  • Hi, I have 2 different field in my form (Timeline and Est. Close Date)

    Example:
    Today is 16/4/2013
    In Timeline I select "This Year"
    So, In Est. Close Date will auto populate date base on Current Date & Timeline, which is 31/12/2013

    Just like :

    Inside my Timeline I have (This Month, This Quarter, Next Quarter, This Year and next year)

    Thank you!!


    Tuesday, April 16, 2013 5:58 AM

Answers

  • Hi KennyChan6996,

    Apply the below javascript on OnChange of the Timeline field.

    function new_timelineOnchange() {
        var This_Month = "This Month";
        var This_Quarter = "This Quarter";
        var Next_Quarter = "Next Quarter";
        var This_Year = "This Year";
        var Next_Year = "Next Year";
        var currentdate = new Date();
        var currentmonth = currentdate.getMonth();
        var currentyear = currentdate.getFullYear();
        var estclosedate;
    
        // Get OPtion set text
        var timeline = Xrm.Page.data.entity.attributes.get("new_timeline");
        var timelineValue = timeline.getSelectedOption().text;
        if (timelineValue != null) {
            // apply to loop for each option 
            switch (timelineValue) {
                case This_Month:
                    // define the logic and set the appropriate date in datetime field based on todays date
                    estclosedate = new Date((new Date(currentyear, currentmonth + 1, 1)) - 86400000);
                    break;
                case This_Quarter:
                    // define the logic and  set the appropriate date in datetime field based on todays date
                    if (currentmonth < 3) {
                        estclosedate = new Date((new Date(currentyear, 3, 1)) - 86400000);
                        break;
                    }
                    if (currentmonth < 6) {
                        estclosedate = new Date((new Date(currentyear, 6, 1)) - 86400000);
                        break;
                    }
                    if (currentmonth < 9) {
                        estclosedate = new Date((new Date(currentyear, 9, 1)) - 86400000);
                        break;
                    }
                    if (currentmonth < 12) {
                        estclosedate = new Date((new Date(currentyear, 12, 1)) - 86400000);
                        break;
                    }
                case Next_Quarter:
                    // define the logic and  set the appropriate date in datetime field based on todays date
                    // define the logic and  set the appropriate date in datetime field based on todays date
                    if (currentmonth < 3) {
                        estclosedate = new Date((new Date(currentyear, 6, 1)) - 86400000);
                        break;
                    }
                    if (currentmonth < 6) {
                        estclosedate = new Date((new Date(currentyear, 9, 1)) - 86400000);
                        break;
                    }
                    if (currentmonth < 9) {
                        estclosedate = new Date((new Date(currentyear, 12, 1)) - 86400000);
                        break;
                    }
                    if (currentmonth < 12) {
                        estclosedate = new Date((new Date(currentyear, 15, 1)) - 86400000);
                        break;
                    }
                    break;
                case This_Year:
                    // define the logic and  set the appropriate date in datetime field based on todays date
                    estclosedate = new Date(currentyear, 11, 31);
                    break;
                case Next_Year:
                    // define the logic and  set the appropriate date in datetime field based on todays date
                    estclosedate = new Date(currentyear+1, 11, 31);
                    break;
            }
    
            Xrm.Page.data.entity.attributes.get("new_estclosedate").setValue(estclosedate);
            // force submit the date field, other wise field will appear is null 
            Xrm.Page.data.entity.attributes.get("new_estclosedate").setSubmitMode("always");
        }
    }


    Techsun
    MSN:caims#techsun.com
    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    • Marked as answer by KennyChan6996 Tuesday, April 16, 2013 8:55 AM
    Tuesday, April 16, 2013 8:41 AM
    Moderator
  • Minus one day,

    60 seconds * 60 minutes * 24hours * 1000 = 86400000 milliseconds


    Techsun
    MSN:caims#techsun.com
    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    • Marked as answer by KennyChan6996 Tuesday, April 16, 2013 9:45 AM
    Tuesday, April 16, 2013 9:04 AM
    Moderator
  • Hi Kenny,

    The code given by Batistuta is perfect.

    Just you can check for null value and set the field to empty value.

    var estclosedate = "";
    
     // Get OPtion set text
    	   var timeline = Xrm.Page.data.entity.attributes.get("new_timeline");
    
    if(timeline != null)
    
    {
    
    var timelineValue = timeline.getSelectedOption().text;
    
    // switch code comes here
    
    }
    
      Xrm.Page.data.entity.attributes.get("new_estclosedate").setValue(estclosedate);
      // force submit the date field, other wise field will appear is null 
      Xrm.Page.data.entity.attributes.get("new_estclosedate").setSubmitMode("always");
    

    --

    Thanks and Regards,

    Gopinath.

    • Marked as answer by KennyChan6996 Wednesday, April 17, 2013 2:20 AM
    Tuesday, April 16, 2013 10:24 AM

All replies

  • Hi,

    Yes, this is very much possible in CRM. Apply the below JS on OnChange of the Proposal type field and OnLOad of the form. Define the logic in below JS to set the date in estimated close date field. 

    var This_Month = "This Month";
    var This_Quarter = "This Quarter";
    var Next_Quarter = "Next Quarter";
    var This_Year = "This Year";
    var Next_Year = "Next Year";
    
    // Get OPtion set text
    var proposalType = Xrm.Page.data.entity.attributes.get("<proposalTypeSchema>"); 
    var proposalTypeValue =proposalType.getSelectedOption().text;
    if (proposalTypeValue != null) { 
    // apply to loop for each option 
     switch (proposalTypeValue) {
            case This_Month:
                // define the logic and set the appropriate date in datetime field based on todays date
            break;
            case This_Quarter:
                // define the logic and  set the appropriate date in datetime field based on todays date
            break;
            case Next_Quarter:
                // define the logic and  set the appropriate date in datetime field based on todays date
            break;
            case This_Year:
            // define the logic and  set the appropriate date in datetime field based on todays date
            break;
            case Next_Year:
            // define the logic and  set the appropriate date in datetime field based on todays date
            break;
    }
    
    // force submit the date field and optionset, other wise field will appear is null 
    Xrm.Page.data.entity.attributes.get("<datefield>").setSubmitMode("always");
    Xrm.Page.data.entity.attributes.get("<optionset>").setSubmitMode("always");
    


    Chandan - http://mscrm-chandan.blogspot.in/ I hope this helps. If my response answered your question, please mark the response as an answer and also vote as helpful !!!

    • Proposed as answer by Andre.K.MMVP Tuesday, April 16, 2013 7:35 AM
    Tuesday, April 16, 2013 6:49 AM
  • erm, I dun really know how "set the appropriate date in Datetime field based on todays date" ?? sorry...!

    Tuesday, April 16, 2013 6:54 AM
  • You can make your own implementation/function on how you would define that, for example: you want to set this month*

    call below function to generate the date for this month:

    function LastDayOfMonth(Month, Year){
         return new Date((new Date(Year, Month, 1))-1);
    }
    * The function was taken from: 

    http://stackoverflow.com/questions/1924815/last-day-of-the-month


    • Edited by Andre.K.MMVP Tuesday, April 16, 2013 7:04 AM add clarity of wording.
    Tuesday, April 16, 2013 7:03 AM
  • Hi,

    Definitly you know the logic, what should be the date value for each proposal type based on todays date.  
    If you know the Java script, then you define the logic to set the date time in estimated close date field for each proposal type.
    Already sample code has been given by  "Andre.K.M"



    Chandan - http://mscrm-chandan.blogspot.in/ I hope this helps. If my response answered your question, please mark the response as an answer and also vote as helpful !!!


    Tuesday, April 16, 2013 7:33 AM
  • erm, I can I have a sample javascript if I select "Next Quarter" for base on current date ???

    Sorry and thank you very much !

    Tuesday, April 16, 2013 7:50 AM
  • Hi KennyChan6996,

    Apply the below javascript on OnChange of the Timeline field.

    function new_timelineOnchange() {
        var This_Month = "This Month";
        var This_Quarter = "This Quarter";
        var Next_Quarter = "Next Quarter";
        var This_Year = "This Year";
        var Next_Year = "Next Year";
        var currentdate = new Date();
        var currentmonth = currentdate.getMonth();
        var currentyear = currentdate.getFullYear();
        var estclosedate;
    
        // Get OPtion set text
        var timeline = Xrm.Page.data.entity.attributes.get("new_timeline");
        var timelineValue = timeline.getSelectedOption().text;
        if (timelineValue != null) {
            // apply to loop for each option 
            switch (timelineValue) {
                case This_Month:
                    // define the logic and set the appropriate date in datetime field based on todays date
                    estclosedate = new Date((new Date(currentyear, currentmonth + 1, 1)) - 86400000);
                    break;
                case This_Quarter:
                    // define the logic and  set the appropriate date in datetime field based on todays date
                    if (currentmonth < 3) {
                        estclosedate = new Date((new Date(currentyear, 3, 1)) - 86400000);
                        break;
                    }
                    if (currentmonth < 6) {
                        estclosedate = new Date((new Date(currentyear, 6, 1)) - 86400000);
                        break;
                    }
                    if (currentmonth < 9) {
                        estclosedate = new Date((new Date(currentyear, 9, 1)) - 86400000);
                        break;
                    }
                    if (currentmonth < 12) {
                        estclosedate = new Date((new Date(currentyear, 12, 1)) - 86400000);
                        break;
                    }
                case Next_Quarter:
                    // define the logic and  set the appropriate date in datetime field based on todays date
                    // define the logic and  set the appropriate date in datetime field based on todays date
                    if (currentmonth < 3) {
                        estclosedate = new Date((new Date(currentyear, 6, 1)) - 86400000);
                        break;
                    }
                    if (currentmonth < 6) {
                        estclosedate = new Date((new Date(currentyear, 9, 1)) - 86400000);
                        break;
                    }
                    if (currentmonth < 9) {
                        estclosedate = new Date((new Date(currentyear, 12, 1)) - 86400000);
                        break;
                    }
                    if (currentmonth < 12) {
                        estclosedate = new Date((new Date(currentyear, 15, 1)) - 86400000);
                        break;
                    }
                    break;
                case This_Year:
                    // define the logic and  set the appropriate date in datetime field based on todays date
                    estclosedate = new Date(currentyear, 11, 31);
                    break;
                case Next_Year:
                    // define the logic and  set the appropriate date in datetime field based on todays date
                    estclosedate = new Date(currentyear+1, 11, 31);
                    break;
            }
    
            Xrm.Page.data.entity.attributes.get("new_estclosedate").setValue(estclosedate);
            // force submit the date field, other wise field will appear is null 
            Xrm.Page.data.entity.attributes.get("new_estclosedate").setSubmitMode("always");
        }
    }


    Techsun
    MSN:caims#techsun.com
    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    • Marked as answer by KennyChan6996 Tuesday, April 16, 2013 8:55 AM
    Tuesday, April 16, 2013 8:41 AM
    Moderator
  • Thank You very much Batistuta Cai..!! but can I know what is "- 86400000" in the javascript ??
    Tuesday, April 16, 2013 8:56 AM
  • Minus one day,

    60 seconds * 60 minutes * 24hours * 1000 = 86400000 milliseconds


    Techsun
    MSN:caims#techsun.com
    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    • Marked as answer by KennyChan6996 Tuesday, April 16, 2013 9:45 AM
    Tuesday, April 16, 2013 9:04 AM
    Moderator
  • Hi,Batistuta Cai...but if Timeline = null.....its will pomp error...what should I add into the js ??
    Tuesday, April 16, 2013 9:58 AM
  • Hi Kenny,

    The code given by Batistuta is perfect.

    Just you can check for null value and set the field to empty value.

    var estclosedate = "";
    
     // Get OPtion set text
    	   var timeline = Xrm.Page.data.entity.attributes.get("new_timeline");
    
    if(timeline != null)
    
    {
    
    var timelineValue = timeline.getSelectedOption().text;
    
    // switch code comes here
    
    }
    
      Xrm.Page.data.entity.attributes.get("new_estclosedate").setValue(estclosedate);
      // force submit the date field, other wise field will appear is null 
      Xrm.Page.data.entity.attributes.get("new_estclosedate").setSubmitMode("always");
    

    --

    Thanks and Regards,

    Gopinath.

    • Marked as answer by KennyChan6996 Wednesday, April 17, 2013 2:20 AM
    Tuesday, April 16, 2013 10:24 AM