locked
Adding to a date in CRM 2011 RRS feed

  • Question

  • I have created a new date field in a form.  I want to use the new field in a work flow and wait until the current date and time is => the date/time in the new field.  Can someone help me with some java code to take the current date and time, add two hours to it, and a random number of minutes between 1 and 99.  The name of the new field is randomprocesstime.


    Thanks, Dale

    Wednesday, September 4, 2013 8:07 PM

Answers

  • Hi,

    You could try something like this:

    function DateManupiilator() { var date = Xrm.Page.getAttribute("randomprocesstime").getValue();

    var randomminute = Math.floor((Math.random()*100)+1);

    var newdate = new Date(date.getFullYear(), date.getMonth(), date.getDay(), date.GetHours() + 2, date.GetMinutes() + randomminute);

    Xrm.Page.getAttribute("field name").setValue(newdate);

    }

    Then you could use this date in your workflow.



    My Weblog | My Website


    Thursday, September 5, 2013 5:30 AM
    Moderator

All replies

  • Dale,

    This can be easily achieved in the workflow using the Date utilities in the Manipulation library. You don't have to write any JScript for this.

    Hope this helps!


    If my response helps you in finding your answer then please click 'Mark as Answer' and 'Vote as Helpful'

    Wednesday, September 4, 2013 9:24 PM
  • Hi,

    You could try something like this:

    function DateManupiilator() { var date = Xrm.Page.getAttribute("randomprocesstime").getValue();

    var randomminute = Math.floor((Math.random()*100)+1);

    var newdate = new Date(date.getFullYear(), date.getMonth(), date.getDay(), date.GetHours() + 2, date.GetMinutes() + randomminute);

    Xrm.Page.getAttribute("field name").setValue(newdate);

    }

    Then you could use this date in your workflow.



    My Weblog | My Website


    Thursday, September 5, 2013 5:30 AM
    Moderator
  • Mamatha,

    I would love to do it in a workflow.  That would solve other issues, but how do I do that random number of minutes?


    Thanks, Dale

    Thursday, September 5, 2013 12:25 PM
  • The random numbers are not supported in workflows. If you want to do it other than a JavaScript, you may write your own workflow activity.


    My Weblog | My Website

    Thursday, September 5, 2013 12:50 PM
    Moderator
  • That looks like a good start but instead of load the var date to the time from a field and it use the system current date and time?  How would that look?


    Thanks, Dale

    Thursday, September 5, 2013 6:41 PM
  • That looks like a good start but instead of load the var date to the time from a field and it use the system current date and time?  How would that look?


    Thanks, Dale

    Replace the first line with this:

    var date = new Date();



    My Weblog | My Website

    Thursday, September 5, 2013 6:47 PM
    Moderator
  • OK - so here is what I have:

    var date = new Date();
    var randomminute = Math.floor((Math.random()*100)+1);
    var newdate = new Date(date.getFullYear(), date.getMonth(), date.getDay(), date.GetHours() + 2, date.GetMinutes() + randomminute);
    alert(newdate);
    Xrm.Page.getAttribute("po_updatetotalsdate").setValue(newdate);

    CRM give me the error:  Object doesn't support property or method 'GetHours'

    Any ideas?


    Thanks, Dale

    Thursday, September 5, 2013 6:55 PM
  • Refer to this post by Leon.

    Other approach is to generate the random number in a workflow activity and use it in the parent workflow.


    If my response helps you in finding your answer then please click 'Mark as Answer' and 'Vote as Helpful'


    Thursday, September 5, 2013 7:00 PM
  • Can you explain that in more detail?

    Thanks, Dale

    Thursday, September 5, 2013 7:01 PM
  • Dale,

    You can write custom workflow activities (basically an assembly) using C#.Net or VB.Net. You could write teh random number generation logic in a custom workflow activity and then wait until the desired time in the main workflow.

    Pls refer the below for a sample custom workflow activity that has date calculations

    http://www.resultondemand.nl/support/sdk/1cff83b0-1f7b-4ddb-a2af-b85f9f785529.htm


    If my response helps you in finding your answer then please click 'Mark as Answer' and 'Vote as Helpful'

    Thursday, September 5, 2013 7:19 PM
  • OK - so here is what I have:

    var date = new Date();
    var randomminute = Math.floor((Math.random()*100)+1);
    var newdate = new Date(date.getFullYear(), date.getMonth(), date.getDay(), date.GetHours() + 2, date.GetMinutes() + randomminute);
    alert(newdate);
    Xrm.Page.getAttribute("po_updatetotalsdate").setValue(newdate);

    CRM give me the error:  Object doesn't support property or method 'GetHours'

    Any ideas?


    Thanks, Dale

    Sorry. That was my mistake. Use getHours() instead of GetHours(). 


    My Weblog | My Website

    Thursday, September 5, 2013 7:40 PM
    Moderator
  • Yep I figured that out.  The interesting thing was even after I figured that out the date it got in the end was back one day.  I fixed it by doing this but I don't understand why I have to:

    var date = new Date();
    var randomminute = Math.floor((Math.random()*100)+1);
    var newdate = new Date(date.getFullYear(), date.getMonth(), date.getDay()+1, date.getHours() + 2, date.getMinutes() + randomminute);
    Xrm.Page.getAttribute("po_updatetotalsdate").setValue(newdate);


    Thanks, Dale

    Thursday, September 5, 2013 7:44 PM