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;
}
});
});