在CRM 2013插件获取日期与错误的时间,但UI没有时间字段.如何从日期中删除时间
本文关键字:时间 字段 没有时间 UI 删除 日期 插件 2013 CRM 获取 取日期 | 更新日期: 2023-09-27 18:11:23
我目前正在通过以下方式从CRM检索日期字符串
Entity dateDeliveryRequiredImage = (Entity)context.PostEntityImages["DeliveryRequired"];
这确实工作,但由于某种原因,它似乎是检索的时间,当我创建一个CSV文件通过以下行
dateDeliveryRequiredImage["requestdeliverby"].ToString()
有没有人知道它是从哪里得到时间的,因为没有地方输入时间(它说现在是晚上11点??)
更重要的是如何摆脱它
谢谢,肖恩
输入所需代码后出现以下错误
if (!timeZoneCode.HasValue)
return;
类型可转换为system的对象。需要Timeanddate
var response = (LocalTimeFromUtcTimeResponse)_serviceProxy.Execute(request);
_serviceProxy在当前上下文中不存在
我的服务代码如下;
IPluginExecutionContext context = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
然而,输入服务或上下文没有任何作用
最后新QueryExpression (UserSettings.EntityLogicalName)
当前上下文中不存在用户设置
代码更新正常
private DateTime RetrieveLocalTimeFromUTCTime(DateTime utcTime, IOrganizationService service)
{
int? timeZoneCode = RetrieveCurrentUsersSettings(service);
if (!timeZoneCode.HasValue)
throw new Exception("Can't find time zone code");
var request = new LocalTimeFromUtcTimeRequest
{
TimeZoneCode = timeZoneCode.Value,
UtcTime = utcTime.ToUniversalTime()
};
var response = (LocalTimeFromUtcTimeResponse)service.Execute(request);
return response.LocalTime;
//var utcTime = utcTime.ToString("MM/dd/yyyy HH:mm:ss");
//var localDateOnly = response.LocalTime.ToString("dd-MM-yyyy");
}
private int? RetrieveCurrentUsersSettings(IOrganizationService service)
{
var currentUserSettings = service.RetrieveMultiple(
new QueryExpression("usersettings")
{
ColumnSet = new ColumnSet("localeid", "timezonecode"),
Criteria = new FilterExpression
{
Conditions =
{
new ConditionExpression("systemuserid", ConditionOperator.EqualUserId)
}
}
}).Entities[0].ToEntity<Entity>();
return (int?)currentUserSettings.Attributes["timezonecode"];
}
请注意与下面代码的不同之处在于:
}).Entities[0].ToEntity<Entity>();
return (int?)currentUserSettings.Attributes["timezonecode"];
CRM将写入CSV之前可以删除的默认时间,如下所示:
DateTime dateAndTime = (DateTime)dateDeliveryRequiredImage["requestdeliverby"];
var date = dateAndTime.ToString("dd-MM-yyyy");
如果您想将默认时间写入CSV。你可以这样使用:
var date = dateAndTime.Date;
Dynamics CRM 2011以UTC时间存储所有DateTime字段,因此通过使用' DateTime。我们的日期将在到达数据库之前被转换成UTC时间。例如,默认时间是12:00:00,在数据库中它将被存储为11:00:00。这就是为什么你在你的插件中得到11PM。当我们的用户通过CRM读取日期时,它将被转换回当地时区,用户将读取正确的时间。
IPluginExecutionContext context = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
// This retrieves the UTC time
DateTime dateAndTime = (DateTime)dateDeliveryRequiredImage["requestdeliverby"];
// This converts the UTC time to your local time
var localDate = RetrieveLocalTimeFromUTCTime(dateAndTime, service);
// It will give you the correct date
var date = dateAndTime.ToString("dd-MM-yyyy");
将UTC转换为本地时间。请检查下面的代码:
/// <summary>
/// Retrive the local time from the UTC time.
/// </summary>
/// <param name="utcTime"></param>
/// <param name="service"></param>
private DateTime RetrieveLocalTimeFromUTCTime(DateTime utcTime, IOrganizationService service)
{
int? timeZoneCode = RetrieveCurrentUsersSettings(service);
if (!timeZoneCode.HasValue)
throw new Exception("Can't find time zone code");
var request = new LocalTimeFromUtcTimeRequest
{
TimeZoneCode = timeZoneCode.Value,
UtcTime = utcTime.ToUniversalTime()
};
var response = (LocalTimeFromUtcTimeResponse)service.Execute(request);
return response.LocalTime;
//var utcTime = utcTime.ToString("MM/dd/yyyy HH:mm:ss");
//var localDateOnly = response.LocalTime.ToString("dd-MM-yyyy");
}
/// <summary>
/// Retrieves the current users timezone code and locale id
/// </summary>
private int? RetrieveCurrentUsersSettings(IOrganizationService service)
{
var currentUserSettings = service.RetrieveMultiple(
new QueryExpression("usersettings")
{
ColumnSet = new ColumnSet("localeid", "timezonecode"),
Criteria = new FilterExpression
{
Conditions =
{
new ConditionExpression("systemuserid", ConditionOperator.EqualUserId)
}
}
}).Entities[0].ToEntity<UserSettings>();
return currentUserSettings.TimeZoneCode;
}
Ref: Sample:检索时区信息