I want to create a contact record whenever an account record is created in power apps portals. when I run the JavaScript I get the error 401 unauthorized.
I have set site settings for both accounts and contacts. I have given entity permissions for both the entities, i have also set web roles for the entity permissions and have also assigned the web roles to contact trying to create the record.
below is my code, i am using web api to create the record.
debugger;
var entity = {};
entity.firstname = "sample";
entity.lastname = "data";
var req = new XMLHttpRequest();
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
//req.setRequestHeader('Access-Control-Allow-Credentials', 'true');
//req.setRequestHeader('Access-Control-Allow-Methods',"POST");
req.onreadystatechange = function() {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 201) {
var uri = this.getResponseHeader("OData-EntityId");
var regExp = /\(([^)]+)\)/;
var matches = regExp.exec(uri);
var newEntityId = matches[1];
} else {
console.log(this.statusText);
}
}
};
req.send(JSON.stringify(entity));
Authenticaion is enabled by local sign in and azure active directory.
The role of my user is system administrator.
what could be the issue?