Hi team,
I have a request when user click on a custom button, it will change the value of a field on an entity in CRM from portal. Basically, I followed the Web API with PUT method and also added the Wrapper before my logic in code. However, it shows 401 each time calling, even I manually get the token from the site to add in.
Please refer my code below and let me know where I am wrong.
URL: https://portallink.powerappsportals.com/quote/quote-details/?id=5c925d18-7109-ed11-82e5-000d3a222fdf
(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)
var url = 'https://xoxoxo.powerappsportals.com/quote/quote-details/?id=5c925d18-7109-ed11-82e5-000d3a222fdf';
var id = url.substring(url.lastIndexOf('/') + 5);
var token = $("#__RequestVerificationToken").val();
$.ajax({
url: 'https://xoxoxo.powerappsportals.com/_api/quotes(' + id + ')/statuscode',
method: 'PUT',
headers: {
'__RequestVerificationToken': token,
},
data: JSON.stringify({ key: 'StatusCode', value: '8' }),
success: function (result) {
console.log(result);
}
});
Thank you.
The error message I have: ""Request validation failed : The required anti-forgery form field \"__RequestVerificationToken\" is not present.""
Thanks.
Hi @lucasluong
Can you also post your console log?
Hope it helps.
------------
If you like this post, give it a Thumbs up. Where it solved your request, Mark it as a Solution to enable other users to find it.
Hi @ragavanrajan ,
Thank you for looking at my issue.
Please help to review the logs here. I have censored all the sensitive info. Thank you for your help in advance.
(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)
var url = 'https://xoxoxo.powerappsportals.com/quote/quote-details/?id=5c925d18-7109-ed11-82e5-000d3a222fdf';
var id = url.substring(url.lastIndexOf('/') + 5);
var token = $("#__RequestVerificationToken").val();
$.ajax({
url: 'https://xoxoxo.powerappsportals.com/_api/quotes(' + id + ')/statuscode',
method: 'PUT',
headers: {
'__RequestVerificationToken': token,
},
data: JSON.stringify({ key: 'StatusCode', value: '8' }),
success: function (result) {
console.log(result);
}
});
{readyState: 1, getResponseHeader: ƒ, getAllResponseHeaders: ƒ, setRequestHeader: ƒ, overrideMimeType: ƒ, …}
preform.bundle-2c5735c41a.js:2 PUT https://digitaltravel.powerappsportals.com/_api/quotes(5c925d18-7109-ed11-82e5-000d3a222fdf)/statuscode 401 (Unauthorized)
send @ preform.bundle-2c5735c41a.js:2
ajax @ preform.bundle-2c5735c41a.js:2
a.ajax @ preform.bundle-2c5735c41a.js:7
(anonymous) @ VM121:35
Hi @Lucas_Luong
You can also use web api to update the record:
var quoteId = "{{request.params.id}}"
webapi.safeAjax({
type: "PATCH",
url: "/_api/quotes("+ quoteId +")",
contentType: "application/json",
data: JSON.stringify({
"statuscode": 8
}),
success: function (res) {
console.log(res);
}
});
--------------------------
If you like this post, give a Thumbs up. Where it solved your query, Mark as a Solution so it can help other people!