在服务实体中创建新记录

时间:2016-04-01 09:06:55

标签: c# dynamics-crm-2016

我已经看到了通过C#创建帐户实体记录,联系人实体记录的示例,我想知道如何通过C#(。net)代码在CRM中创建服务记录。

例如:我们已在服务实体视图中显示“管道服务”记录。所以我想通过C#代码在服务实体中创建一条新记录(早期或晚期绑定无关紧要)。

有人可以通过代码帮助我。

1 个答案:

答案 0 :(得分:4)

从代码创建此服务时需要一些XML。此外,在创建服务之前,您需要创建ResourceSpec和ConstraintBasedGroup。

首先创建一个ConstraintBasedGroup:

var bu = context.BusinessUnitSet.First().ToEntityReference();

var cbg = new ConstraintBasedGroup
{
    BusinessUnitId = bu,
    Name = "CBG1",
    Constraints = "<Constraints><Constraint><Expression><Body>false</Body><Parameters><Parameter name=\"resource\"/></Parameters></Expression></Constraint></Constraints>"
};
var cbgId = OrganizationService.Create(cbg);

然后创建ResourceSpec:

var resSpec = new ResourceSpec
{
    BusinessUnitId = bu,
    Name = "RS1",
    RequiredCount = 1,
    ObjectiveExpression = "<Expression><Body>udf\"Random\"(factory,resource,appointment,request,leftoffset,rightoffset)</Body><Parameters><Parameter name=\"factory\"/><Parameter name=\"resource\"/><Parameter name=\"appointment\"/><Parameter name=\"request\"/><Parameter name=\"leftoffset\"/><Parameter name=\"rightoffset\"/></Parameters><Properties EvaluationInterval=\"P0D\" evaluationcost=\"0\"/></Expression>",
    GroupObjectId = cbgId
};
var resSpecId = OrganizationService.Create(resSpec);

最后,您可以创建服务:

var svc = new Service
{
    Name = "Service1",
    Granularity = "FREQ=MINUTELY;INTERVAL=15",
    ResourceSpecId = new EntityReference(ResourceSpec.EntityLogicalName, resSpecId),
    InitialStatusCode = new OptionSetValue(0),
    Duration = 15
};
OrganizationService.Create(svc);

我建议您使用CRM的UI创建类似的东西,以防您想知道所需的XML的特定格式。我在我的示例中使用的XML几乎是XML CRM生成的默认值。