Показать сообщение отдельно
Старый 04.10.2011, 11:50   #1  
andrey. is offline
andrey.
Участник
 
7 / 10 (1) +
Регистрация: 04.10.2011
Определение давности заказа (CRM2011)
Коллеги, добрый день!
Подскажите, пожалуйста, по такому вопросу: мне необходимо определить давность заказа в месяцах (Текущая дата - дата создания заказа). Для этого в форму заказа добавлено поле orderAge. C определением текущего месяца вроде все ясно.
Подскажите, как получить значение даты заказа и для какого события регистрировать плагин. Заранее спасибо!

П.С. CRM2011, ниже приведенный код использовал для Retrieve, Pre-operation.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
// Microsoft Dynamics CRM namespace(s)
using Microsoft.Xrm.Sdk;
using System.ServiceModel;
using Microsoft.Xrm.Sdk.Query;


namespace CRMTestPlugIn
{
public class TestPlugIn : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
// Obtain the execution context from the service provider.
Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)
serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));

if (context.Depth == 1)
{

IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

// Obtain the target entity from the input parmameters.
EntityReference entity = (EntityReference)context.InputParameters["Target"];

//OrderAge
ColumnSet cols = new ColumnSet(new String[] { "new_orderage"});
var order = service.Retrieve("salesorder", entity.Id, cols);
if (order != null)
{
if (order.Attributes.Contains("new_orderage") == false)
{

var todayMonth = DateTime.Today.Month + DateTime.Today.Year * 12;
order.Attributes.Add("new_orderage", todayMonth.ToString());
}
else
{
var todayMonth = DateTime.Today.Month + DateTime.Today.Year * 12;
order["new_orderage"] = todayMonth.ToString();
}
service.Update(order);
}

}
}
}//end class
}//end name space