简体   繁体   中英

create a new record in service entity

I have seen example of creating Accounts Entity records, Contacts entity records through C#, i wanted to know how do we create a service record in CRM through C#(.net) code.

Eg: We already have "Plumbing service" record in service entity view. So i wanted to create a new record in service entity through C# code (early or late binding doesn't matter).

Can someone help me on this with code.

Quite some XML is required when creating this Services from code. Additionally, before you can create a Service you will need to create a ResourceSpec and a ConstraintBasedGroup.

First create a 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);

Then create a 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);

And finally, you can create your Service:

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);

I would suggest you create similar things using the UI of CRM in case you are wondering about the specific formats of the XML you require. The XML I used in my examples is pretty much the default XML CRM generates.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM