4

I am trying to add time counter to standard survey form and save elapsed time with coresponding username at another list so I just mixed up some code which takes current user, elapsed time and creates new item on new list called in this case "Wyniki" and now I have time and current username as I wanted but...

There is a small problem. When two or more users end survey at exact same time lets say 12:32:33(this is not elapsed time) I get only one completion time but two proper answers on main survey list. There is no problem with saving answers in main survey list but I have problem with second list and saving more elements at the same time. Is there a way to prevent that?

Is it possible to save two, three, ten elements on list at exact same time using $.ajax? or SPServices?

var start = new Date;
var elapsedTime = 0; // in seconds
var userid = _spPageContextInfo.userId;
var loginName;

setInterval(function() {
    elapsed = (new Date - start)/1000;
    //console.log(elapsed);
}, 10);

function GetCurrentUser() {
    var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")";

    var requestHeaders = { "accept" : "application/json;odata=verbose" };

    $.ajax({
        url : requestUri,
        contentType : "application/json;odata=verbose",
        headers : requestHeaders,
        success : onSuccess,
        error : onError
    });
}

function onSuccess(data, request){
    loginName = data.d.Title;
}

function onError(error) {
    alert(error);
}
GetCurrentUser();


function PreSaveAction() {
    var listName = "Wyniki";
    var imieNazwisko = loginName;
    var czas = elapsed;
    CreateListItemWithDetails(listName, _spPageContextInfo.webAbsoluteUrl, imieNazwisko, czas, function () {


    }, function () {
        //alert("Niestety wystąpił błąd. Spróbuj ponownie.");
    });
    return true;
};

// CREATE Operation
// listName: The name of the list you want to get items from
// weburl: The url of the web that the list is in.
// newItemTitle: New Item title.
// success: The function to execute if the call is sucesfull
// failure: The function to execute if the call fails
function CreateListItemWithDetails(listName, webUrl, imieNazwisko, czas, success, failure) {
    var itemType = GetItemTypeForListName(listName);
    var item = {
        "__metadata": { "type": itemType },
        "Title": imieNazwisko,
        "Czas": czas
    };

    $.ajax({
        url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items",
        type: "POST",
        contentType: "application/json;odata=verbose",
        data: JSON.stringify(item),
        headers: {
            "Accept": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val()
        },
        success: function (data) {
            success(data);
        },
        error: function (data) {
            failure(data);
        }
    });
}

// Get List Item Type metadata
function GetItemTypeForListName(name) {
    return "SP.Data." + name.charAt(0).toUpperCase() + name.split(" ").join("").slice(1) + "ListItem";
}
4

Creating items in lists is actually an asynchronous process. When you commit your changes, the item is written, but there's no way to guarantee when the item will be written. I'm sure of this because of the way I had to write the SPRedirectWithID function. As you might expect, it's generally very quickly after the submit, but it depends on server load, etc.

  • I thought there was a lot of example code out there to swap out the original button with a custom one and control when PreSaveAction would be fired. – iOnline247 Feb 20 '16 at 21:42
  • Swapping out the button wouldn't make the new items save at exactly the same time, though. You've still got a lag there. – Marc D Anderson Feb 21 '16 at 1:25
  • I'm on my phone, so it's tough to tell for sure,but I think the PreSaveAction is returning true prior to the async call finishing. – iOnline247 Feb 21 '16 at 3:48

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.