Reputation: 95
I am having an issue with creating a contract from a custom workflow activity. The contract takes it's activeon date from a field in a related entity. The time portion of the date is removed before being set as the activeon date for the contract. This seems to work as expected but the code is creating wonky dates for the contract. Does anyone have any experience with this?
DateTime startDate;
DateTime endDate;
Anh_home home = <<Get record from service>>;
startDate = home.Anh_ActualPossessionDate.GetValueOrDefault().Date;
endDate = startDate.AddYears(contractDuration).Date.AddSeconds(-1);
newContract = new Contract()
{
Title = contractName,
anhwp_Home = home.ToEntityReference(),
CustomerId = home.Anh_CompanyId,
ActiveOn = startDate,
ExpiresOn = endDate,
BillingCustomerId = home.Anh_CompanyId,
BillingStartOn = startDate,
BillingEndOn = endDate,
ContractTemplateId = dataContext.ContractTemplateSet.FirstOrDefault(x => x.Abbreviation == "HWC").ToEntityReference()
};
newContract.Id = service.Create(newContract);
Below is the results of a query on the filtered views. It seems like the activeon date is setting the activeonutc field but my understanding is that CRM handles the conversion to UTC based on the users settings.
Thanks.
Upvotes: 0
Views: 81
Reputation: 95
So I have stumbled upon the answer to my question on this great blog post https://community.dynamics.com/crm/b/develop1/archive/2011/12/06/dynamics-crm-datetimes-the-last-word.
It seems that when you retrieve data from CRM via the SDK the date is always sent as UTC but you can send data either as local time or UTC. The issue I was having is that I was treating the date retrieved from the other entity as local time when it was really UTC. So when I saved my new contract CRM was correctly taking the UTC time I provided and converting it to local time.
To fix this issue I simply made a call to the .ToLocalTime() method of the DateTime object of the retrieved record.
Upvotes: 1