2016-04-01 29 views
1

我見過創建賬戶實體記錄的例子,通過C#聯繫人實體記錄,我想知道我們如何通過C#(.net)代碼在CRM中創建服務記錄。在服務實體中創建新記錄

例:我們已經在服務實體視圖中有「水暖服務」記錄。所以我想通過C#代碼在服務實體中創建一個新記錄(早期或晚期綁定無關緊要)。

有人可以幫我解決這個問題。

+0

您是否已嘗試簡單地採用帳戶和聯繫人的示例?你有沒有遇到任何問題?你應該在這裏尋求代碼之前嘗試。 – Filburt

+0

是的,我已經試過並測試過 – Rahul

回答

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

我會建議你創建類似如果您想知道XML的特定格式,則可以使用CRM的UI你需要。我在示例中使用的XML幾乎是默認的XML CRM生成的。

+3

你會在SDK中找到一些額外的聯繫人:SampleCode \ CS \ BusinessDataModel \ ScheduleAndAppointment \ ScheduleResource.cs。但是就此而言,關於這個話題的文檔並不多。 @Henrik可能提供了最好的文檔,你會發現這個答案! – Nicknow

+0

工程就像一個魅力。 @亨利克:謝謝你的詳細解釋。 – hdoghmen

+0

謝謝!亨裏克。 :) – Rahul

相關問題