I have written code using fetch to return a Promise to Dynamics in an Enable Rule, but I am not receiving the correct data in return to Dynamics.
If I test with a simple function like this in Dynamics it works:
function enableButton() { return new Promise(function (resolve, reject) { var test = true; resolve(test); }, function (error) { reject(error.message); console.log(error.message); } ); }
If I do it like this it does not return the promise correct:
function enableButton() { fetch(accesstokenUrl, { method: 'POST', body: headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }) .then(function (response) { return response.json(); }) .then(function (resJson) { return fetch(url, { method: 'POST', headers: { 'Authorization': 'Bearer ' resJson.access_token, 'Content-Type': 'application/json' }, body: JSON.stringify( { "queryparams": true }) }); }) .then(function (res) { return res.json(); }) .then(function (data) { const hasDebtCase = getDebtCase(data); if (hasDebtCase) { return Promise.resolve(hasDebtCase); } else { return Promise.reject(data); } }) .catch((error) => { console.error('Error:', error); }) };
What is the problem with this?