Convert Date from UTC to Time Zone of User

The Date/Time fields stored in CRM with the UTC format, and the Time Zone Code associated with each User in the system settings.
To get the Date/Time field from CRM with the Time Zone of specific user, we must pass the Guid of the user as parameter to get the TimeZoneInfo.


public static DateTime ConvertTimeFromUtc(OrganizationService service, Guid userid, DateTime dateToconvert)
{
//replace userid with id of user
Entity userSettings = service.Retrieve("usersettings", userid, new ColumnSet("timezonecode"));
//timezonecode for UTC is 85
int timeZoneCode = 85;
//retrieving timezonecode from usersetting
if ((userSettings != null) && (userSettings["timezonecode"] != null))
{
timeZoneCode = (int)userSettings["timezonecode"];
}
//retrieving standard name
var qe = new QueryExpression("timezonedefinition");
qe.ColumnSet = new ColumnSet("standardname");
qe.Criteria.AddCondition("timezonecode", ConditionOperator.Equal, timeZoneCode);
EntityCollection TimeZoneDef = service.RetrieveMultiple(qe);
TimeZoneInfo userTimeZone = null;
if (TimeZoneDef.Entities.Count == 1)
{
String timezonename = TimeZoneDef.Entities[0]["standardname"].ToString();
userTimeZone = TimeZoneInfo.FindSystemTimeZoneById(timezonename);
}
//converting date from UTC to user time zone
DateTime cstTime = TimeZoneInfo.ConvertTimeFromUtc(dateToconvert, userTimeZone);
return cstTime;
}

Advertisements

Change the CreatedOn Value

To change the value of createdOn value in MS dynamics CRM with c# :

you must use the early bound to change the value of createdOn
example :
I want to creat a phonecall entity with specific value of createdOn field:

PhoneCall phonecall = new phonecall();
phonecall.Subject = "Communication";
phonecall["createdon"]=new date ("19/07/2016");
phonecall["overriddencreatedon"] = new date("19/07/2016);

service.create(phonecall);

You must change the overriddencreatedon  to change the createdOn.

I used MS dynamics CRM 2016.

Change the CreatedOn Value

To change the value of createdOn value in MS dynamics CRM with c# :

you must use the early bound to change the value of createdOn
example :
I want to created a phonecall entity with specific value of createdOn value:

PhoneCall phonecall = new phonecall();
phonecall.Subject = "Communication";
phonecall["createdon"]=new date ("19/07/2016");
phonecall["overriddencreatedon"] = new date("19/07/2016");

service.create(phonecall);

You must change the overriddencreatedon to change the createdOn.

I used MS dynamics CRM 2016.