I am having an issue with a user model not updating with the correct ParentAccount ID after changing the value while the application is running.
For Example
I will run the application and change the contacts parent account. Then create an order and use the "Create" method below to assign the parent account to the order.
Now I feel it should run the "GetUser" method which updated the currentuser model and then takes current user and assign it to the salesorder.
Instead its like it skips that and runs the code below it first and never updates it with the correct Parent Account Id.
Does anyone have any suggestions on why this would happen?
Thanks!
public void Create(CrmContextCore _crmContext, Guid productId, ClaimsPrincipal User)
{
// User Model
UserEntityModel currentuser;
DAL_UserEntity UserData = new DAL_UserEntity();
var EmailAddress = User.Claims.FirstOrDefault(c => c.Type == System.Security.Claims.ClaimTypes.Email)?.Value;
var salesorder = new Entity("salesorder");
{
// Go get the current user data from crm system
currentuser = UserData.GetUser(_crmContext, EmailAddress);
// ISSUE! If i change this value while the application is running and rerun the method it shows the old value of currentuser not the new one??
salesorder["customerid"] = new EntityReference("account", currentuser.ParentAccount.Id);
salesorder["contactid"] = new EntityReference("contact", currentuser.ContactId);
salesorder["emailaddress"] = currentuser.Email;
salesorder["name"] = "PO123";
}
_crmContext.ServiceContext.AddObject(salesorder);
_crmContext.ServiceContext.SaveChanges();
}
Here is the User Model
public class UserEntityModel
{
public Guid ContactId {get; set;}
public EntityReference ParentAccount { get; set; }
public Guid Account {get; set;}
public string FirstName {get; set;}
public string LastName {get; set;}
public string Email {get; set;}
}
Here is where the User model is created
public class DAL_UserEntity
{
public UserEntityModel GetUser(CrmContextCore _crmContext, string email)
{
Console.WriteLine("GetUser Method is Running!!");
var user = (from u in _crmContext.ServiceContext.CreateQuery("contact")
where u.GetAttributeValue<string>("emailaddress1") == email
select u).Single();
UserEntityModel ctx = new UserEntityModel();
ctx.FirstName = user.GetAttributeValue<string>("firstname");
ctx.LastName = user.GetAttributeValue<string>("lastname");
ctx.Email = user.GetAttributeValue<string>("emailaddress1");
ctx.ContactId = user.GetAttributeValue<Guid>("contactid");
ctx.ParentAccount = user.GetAttributeValue<EntityReference>("parentcustomerid");
return ctx;
}
}