10

I have two SP2013 workflows that I built in SP Designer. They both do pretty much the same thing, but for different fields.

In both of them I set a DateTime workflow variable to Today.

One of them gets the date and the time, the other one only gets the date (and shows a time of 12:00AM).

How can I ensure that I capture the current time as well as the date?

I've read a bunch of posts that use a trick where the workflow updates something on the item, and then immediately grabs the CurrentItem:Modified value. I really don't want to do that.

I read another post that suggested getting the time that the workflow started form the workflow context, which should be reasonably close to "now". That would be acceptable to me but I don't see anything like a "time" or "started" option available in the workflow context menu.


More details:

These are two workflows on the same list, each set up to operate on different fields. The underlying field type that they are saving to is Note (plain text), but I am using CSR to create a kind of custom field type. The purpose of the field is to expose a button in the View and Edit forms whereby a user can click the button to send a specific email notification. I wire up the button to start the workflow via the REST API. The workflow sends the email, and then saves a time stamp of when the notification was sent in the note field. Multiple time stamps are allowed and are pipe separated. Then, back in the UI, the rendering code reads all the time stamps and displays a list of the times the notification email was sent, next to the button.

Originally I was trying to save the time stamps as ISO formatted, and then render them in the UI using Date.toLocaleString(), but I noticed that included seconds, and I though it didn't look good and was more detail than was really needed, so I changed the workflow to save the time stamps as ShortDate ShortTime, so it's pretty much pre-formatted text at that point and no need to re-parse it through a Date object on the client side.

Here's what it looks like: enter image description here

The first dates are still in ISO format because I changed up how I store and show the values. But, as you can see, the first field captures an accurate time, and the second one does not.

  • I eventually figured out a workaround for this - instead of trying to get the time from within the workflow, I create a new Javascript Date object and pass that to the workflow as an initiation parameter. That way both workflows consistently get the correct date and time of when they run. – Dylan Cristy Jul 12 '16 at 17:57
1

Are you sure that you have set both variables to return data in the correct format?

Using a workflow variable of the type Date, this is your available options. (Site is for 2010 but it's the same for 2013 workflows.)

  • As String: 8/2/2010 12:00:00 AM
  • ISO Formatted: 2010-08-02T06:00:00Z (Zulu – GMT)
  • Long Date: Monday, August 02, 2010
  • Long Time: 12:00:00 AM
  • Short Date: 8/2/2010
  • Short Time: 12:00 AM

To me it looks like your second variable is used as a string, hence the wrong time.

SharePoint Designer 2010 Workflow Field Formats

  • I am doing the same process with both workflows and variables. I am in fact trying to get an ISO formatted string, but you can't just ISO format Today directly, so I had to put Today into a DateTime variable, then save that out as an ISO string. Again, the problem is that in one workflow, that process captures the current time when the variable is set to Today, and in the other one it is always 12AM (i.e. no "time" portion of the DateTime is captured.) – Dylan Cristy Feb 12 '16 at 13:48
  • Could you add some more details? Are there 2 different lists or the same list? What type of column are you trying to save the date to? – Christoffer Feb 12 '16 at 13:55
  • Updated question with more details. – Dylan Cristy Feb 12 '16 at 14:17
1

It's not perfect but in many instances can help

instead of today use Workflow context:Date and Time Started

enter image description here

  • That is not an option in 2013 workflows. – Dylan Cristy Aug 1 '16 at 13:20
  • I used in SPD 2013 – WantToDo Aug 2 '16 at 14:11
  • Yes, but SP Designer 2013 can be used to create SP 2010 Workflow Engine workflows, or 2013 Workflow Engine workflows. That option is only available in 2010 Engine workflows. – Dylan Cristy Aug 2 '16 at 14:13
1

Someone recently asked me about how I did the workaround I mention in my comment above, so here's how it works.

First, I'd just like to say that if it works for your situation to have the workflow start automatically on item update, I would highly recommend doing that and using the date and time from the item's Modified date. Much easier than this.

However, in my situation, I needed to be able to start the workflow manually, and it was not necessarily associated with updating the item.

So this is for a 2013 workflow, set to allow manual start. You will also need to create an initiation parameter of type DateTime in order to pass the current date/time into the workflow.

A couple other notes about this script:

  • I am using jQuery to make the REST calls. If you can't use jQuery, you will have to modify it to make the REST calls some other way.
  • This script was designed to run on an Item Display form, so certain things are taken for granted, such as that you will be able to get the List ID from the _spPageContextInfo, and that the item's ID will be available in the query parameters. In order for this method to work, you will need to know both the List ID (GUID) and the Item ID (integer).
// first step, get the workfow subscriptions attached to the list
var listID = _spPageContextInfo.pageListId;
var restUri = _spPageContextInfo.webAbsoluteUrl + "/_api/SP.WorkflowServices.WorkflowSubscriptionService.Current/EnumerateSubscriptionsByList('" + listID + "')";
$.ajax({
    url: restUri,
    type: 'POST',
    headers: {
        "accept": "application/json;odata=verbose",
        "X-RequestDigest": $("#__REQUESTDIGEST").val()
    }
}).done(function (data) {

    // get our item ID
    var itemID = GetUrlKeyValue('ID');

    // iterate through the workflow subscriptions
    // and find the one we want by name
    for (var i = 0; i < data.d.results.length; i++) {
        if (data.d.results[i].Name.indexOf("My Workflow Name") > -1) {
            var wfSubscription = data.d.results[i];
            break;
        }
    }

    // SharePoint uses dates in UTC, so we need to adjust
    // for the timezone before sending the DateTime object
    var now = new Date();
    now.setMinutes(now.getMinutes() - now.getTimezoneOffset());

    // create the JSON object that will get passed in as the
    // workflow initiation parameter.  the "Key" value should be
    // the name of the initiation param you set up for your workflow.
    var initParams = {
        "payload": [{
            "Key": "StartTime",
            "Value": now,
            "ValueType": "Edm.DateTime"
        }]
    };

    // start the workflow
    var restUri = _spPageContextInfo.webAbsoluteUrl + "/_api/SP.WorkflowServices.WorkflowInstanceService.Current/StartWorkflowOnListItemBySubscriptionId(subscriptionId='" + wfSubscription.Id + "',itemId='" + itemID + "')";
    $.ajax({
        url: restUri,
        type: 'POST',
        data: JSON.stringify(initParams),
        headers: {
            "accept": "application/json;odata=verbose",
            "content-type": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val()
        },
        dataType: 'json'
    }).done(function (successData) {
        // the workflow was successfully started
    }).fail(function (error) {
        // something went wrong
    });
});

And that's it. Your workflow is started with the current date and time available in the StartTime initiation param.

I'm a little more experienced with the REST API now, so if I were writing this now I would probably try to use $filter and $select on the first REST call to just get the subscription ID of the workflow I wanted, without having to iterate through all the workflow subscriptions for the list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.