Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Power Pages - General Discussions
Unanswered

Updating a choice field using web api

(0) ShareShare
ReportReport
Posted on by 125

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.

 

error shows

 
  1. Request Method:
    PATCH
  2. Status Code:
    404 Not Found
  3.  
    Referrer Policy:
    strict-origin-when-cross-origin

Payload {new_level: "100000001"}

new_level: "100000001"

 

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

}

 

 

  • MH-02091453-0 Profile Picture
    125 on at
    Re: Updating a choice field using web api

    never mind - just realised I hadn't added site settings for the entity...fixed now 🙂

     

    😂

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Michael Gernaey – Community Spotlight

We are honored to recognize Michael Gernaey as our June 2025 Community…

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard > Power Pages

#1
Fubar Profile Picture

Fubar 69 Super User 2025 Season 1

#2
oliver.rodrigues Profile Picture

oliver.rodrigues 49 Most Valuable Professional

#3
Jon Unzueta Profile Picture

Jon Unzueta 43