0

I am using SharePoint 2013 Enterprise. I have 3 List. Now I am creating custom form which fully in jQuery and javascript.

From the 3 list, 2 list works as detail so I have two tables where we can add more than 1 record. At the time of saving record I am checking that this 2 list have attachment or not, if they don't have then I am deleting record and showing alert message. I don't have any error and when I debug it works smoothly as expected.

But issue is that when I run without debugging it does not show alert message.

Below code is my common code for adding record:

function CreateListItemWithDetails(listName, newItem) {
    return $.ajax({
        url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items",
        type: "POST",
        contentType: "application/json;odata=verbose",
        data: JSON.stringify(newItem),
        headers: {
            "Accept": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val()
        }
    });
}
function GetItemTypeForListName(name) {
    return "SP.Data." + name.charAt(0).toUpperCase() + name.split(" ").join("").slice(1) + "ListItem";
}
function getAttachments(listName, _Id) {
        return $.ajax({
            url: "/_api/web/lists/getbytitle('" + listName + "')/items(" + _Id + ")/AttachmentFiles",
            method: "GET",
            headers: { "Accept": "application/json; odata=verbose" }
        });
    }

This is my submit code, to make it short I will put main code only:

$("#btnSubmit").click(function () {
    var errMsg = "Following error occurd.\n", errCnt = 0;
            var listName = "GatePass";
            var itemType = GetItemTypeForListName(listName);
            var area = new Array();
            var newItem = {
                "__metadata": { "type": itemType },
                "Title": $("#txCompanyName").val(),
                "Area": area.join(", "),
                "FromDate": $("#fromDate").val(),
                "ToDate": $("#toDate").val(),
                "Approver": $("#approver").val(),
                "OtherOldBuildingArea": $("#txOldOtherAreas").val(),
                "OtherNewBuildingArea": $("#txNewOtherAreas").val()
            };
            CreateListItemWithDetails(listName, newItem).
                then(function (data) {
                    var gatePassID = data.d.ID;
                    $('#gatePass >tbody >tr').each(function (i, row) {
                        var $tds = $(this).find('td')
                        var updateItemGatePassDetails = {
                            "__metadata": { "type": GetItemTypeForListName("GatePassDetails") },
                            "GatePassIDId": gatePassID.toString()
                        };
                        getAttachments("GatePassDetails", $tds.eq(6).text())
                            .then(function (data) {
                                var attachments = data.d.results;
                                if (attachments.length == 0) {
                                    errCnt++;
                                    errMsg += errCnt.toString() + ". Missing civil id / passport document for " + $tds.eq(0).text() + "\n";
                                }
                            });
                        UpdateListItemWithDetails("GatePassDetails", updateItemGatePassDetails, $tds.eq(6).text());
                    });

                    $('#vehicle >tbody >tr').each(function (i, row) {
                        var $tds = $(this).find('td')
                        var updateItemGatePassVehicleDetails = {
                            "__metadata": { "type": GetItemTypeForListName("GatePassVehicleDetails") },
                            "GatePassIDId": gatePassID.toString()
                        };
                        getAttachments("GatePassVehicleDetails", $tds.eq(6).text())
                            .then(function (data) {
                                var attachments = data.d.results;
                                if (attachments.length == 0) {
                                    errCnt++;
                                    errMsg += errCnt.toString() + ". Missing vehicle document for " + $tds.eq(0).text() + "\n";
                                }
                            });
                        UpdateListItemWithDetails("GatePassVehicleDetails", updateItemGatePassVehicleDetails, $tds.eq(6).text());
                    });
                    if (errCnt > 0) {
                        DeleteListItemWithDetails(listName, gatePassID);
                        alert(errMsg);
                        return false;
                    }
                });
});
  • way ago I had a similar problem where console.log caused an error if the developer tools where not opened. I found it by using some dirty UI debugging. Maybe this can get you some clarity too. Just write the debugging-info into an element and check where your code stops to work. You could also investigate the network traffic in the developer tools to check your requests. – Mx. Apr 7 '16 at 8:07
  • I am not using console.log in my codes. I just re-verified – Milind Apr 7 '16 at 8:17
  • I was just pointing out a way where you could do some pseudo debugging without actual debugging. This way you should be able to explain the behavior of your code. – Mx. Apr 7 '16 at 8:18
  • umm let me try :) – Milind Apr 7 '16 at 8:19
1

I only browsed your code

You have to look closely at WHERE inside the then resolves your code runs, ie. if (errCnt>0) will run before attachments are found.

Also try to merge your code, you have 2 branches that almost do the similar, merge them into one... your code will become smaller.. your code will be easier to debug

  • Where I can merge the code. Kindly can you help me on that. I will appreciated – Milind Apr 7 '16 at 8:26
  • Is it possible that can you give me sample code. Please :) – Milind Apr 7 '16 at 8:31
  • Just look at the 2 places for vehicle and gatepass where you copy/pasted lines of code and only made 3 changes... Make that into one function, like you did yourself for the GetItemTypeForListName function... BTW... no need for that seperate function anymore as that one line will become a line in the new GetDetails function. You will then see the issue is: You do not use the resolve of the getattachments call; but blindly continue before code was processed. – Danny '365CSI' Engelman Apr 7 '16 at 8:32
  • I found the issue but I don't know how to resolve it. If somebody can help me. when I check value of errCnt after errCnt ++ it will show me 1 but once it comes out of the that function it becomes zero. Why is it so !! – Milind Apr 7 '16 at 9:46
  • Because GetAttachments returns a Promise you ignore, if you check for errCnt before that function resolves properly ofcourse it is 0 – Danny '365CSI' Engelman Apr 7 '16 at 10:29

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.