Hi, I am trying to update a choice field using the web api but keep getting error.
My table has a column 'new_levels' and i want the user to be able to update this field from a dropdown. I have the dropdown working correctly but when I try the put it keeps giving errors and I am not sure why. I had a similar issue with a yes/no field ...to solve this i change their values to true/false instead of yes/no and it work...so i tried to change values of my choice to 100000000, etc but it still wont accept the update.
(function(webapi, $){
function safeAjax(ajaxOptions) {
var deferredAjax = $.Deferred();
shell.getTokenDeferred().done(function (token) {
// add headers for ajax
if (!ajaxOptions.headers) {
$.extend(ajaxOptions, {
headers: {
"__RequestVerificationToken": token
}
});
} else {
ajaxOptions.headers["__RequestVerificationToken"] = token;
}
$.ajax(ajaxOptions)
.done(function(data, textStatus, jqXHR) {
validateLoginSession(data, textStatus, jqXHR, deferredAjax.resolve);
}).fail(deferredAjax.reject); //ajax
}).fail(function () {
deferredAjax.rejectWith(this, arguments); // on token failure, pass the token ajax and args
});
return deferredAjax.promise();
}
webapi.safeAjax = safeAjax;
})(window.webapi = window.webapi || {}, jQuery)
//custom code
var globalArray = [];
function pushArray(currentObject) {
var str = currentObject.id;
var res = str.split("|");
var capId = res[1];
var checkbox = document.getElementById("id|" + capId).checked;
var levelvalue = document.getElementById("level|" + capId);
var level = levelvalue.options[levelvalue.selectedIndex].value
console.log(level)
if( level == '0 - None'){
level = "100000000";
}
if( level == '1 - Trained (Only)'){
level = "100000001";
}
if( level == '2 - Capable'){
level = "100000002";
}
if( level == '3 - Capable/Certified'){
level = "100000003";
}
// }
console.log(level);
let objIndex = globalArray.findIndex((obj => obj.id == capId));
if (objIndex == -1) {
globalArray.push({
id: capId,
level: level,
checkboxval: checkbox
})
} else {
globalArray[objIndex].level = level;
globalArray[objIndex].checkboxval = checkbox;
}
}
function pushArrayCheckBox(currentObject) {
var str = currentObject.id;
var res = str.split("|");
var capId = res[1];
var checkbox = document.getElementById("id|" + capId).checked;
var value = document.getElementById("level|" + capId);
var level = value.options[value.selectedIndex].value;
if( level == '0 - None'){
level = "100000000";
}
if( level == '1 - Trained'){
level = "100000001";
}
if( level == '2 - Capable'){
level = "100000002";
}
if( level == '3 - Capable/Certified'){
level = "100000003";
}
let objIndex = globalArray.findIndex((obj => obj.id == capId));
if (objIndex == -1) {
globalArray.push({
id: capId,
checkboxval: checkbox,
level:level
})
} else {
globalArray[objIndex].checkboxval = checkbox;
globalArray[objIndex].level = level;
}
}
function update() {
var i;
for (i = 0; i < globalArray.length; i++) { //update checked row changes
alert( globalArray[i].level + "-" + globalArray[i].checkboxval );
if (globalArray[i].checkboxval == true) //true
{
document.getElementById("processingMsg").innerHTML = "Processing....";
try {
let value = {
"new_capabilitylevel": globalArray[i].level
};
webapi.safeAjax({
type: "PATCH",
url: "/_api/new_capabilitieses(" + globalArray[i].id + ")",
contentType: "application/json",
data: JSON.stringify(value),
success: function(res) {
document.getElementById("processingMsg").innerHTML = "Records updated successfully.";
}
});
}
catch (e) {
alert(e.message);
}
}
}
//refresh();
}