无法在CRM中创建记录

时间:2018-01-03 04:47:34

标签: c# dynamics-crm microsoft-dynamics

我想在CRM中创建一个赢得机会,但我收到了错误。

错误:

  

附加信息:3不是州代码的有效状态代码   OpportunityState.Open on Opportun with Id   8e99128b-3ef0-e711-8145-e0071b6641f1。

代码:

 public void CreateOpportunity()
    {
        Entity opportunity = new Entity("opportunity");
        opportunity["name"] = "ABC";           
        opportunity["statecode"] = new OptionSetValue(1);
        opportunity["statuscode"] = new OptionSetValue(3);
        crmService.Create(opportunity);
    }

2 个答案:

答案 0 :(得分:1)

在CRM中,状态代码只读。添加记录时无法动态设置它,因此您的问题就会出错。

要通过它,您需要使用SetStateRequest

初始化一个新的SetStateRequest类并从msdn示例中设置它:

// Create the Request Object
SetStateRequest state = new SetStateRequest();

// Set the Request Object's Properties
state.State = new OptionSetValue((int)IncidentState.Active);
state.Status = 
    new OptionSetValue((int)incident_statuscode.WaitingforDetails);

// Point the Request to the case whose state is being changed
state.EntityMoniker = caseReference;

// Execute the Request
SetStateResponse stateSet = (SetStateResponse)_serviceProxy.Execute(state);

// Check if the state was successfully set
Incident incident = _serviceProxy.Retrieve(Incident.EntityLogicalName, 
    _caseIncidentId, new ColumnSet(allColumns: true)).ToEntity<Incident>();

if (incident.StatusCode.Value == (int)incident_statuscode.WaitingforDetails)
{
    Console.WriteLine("Record state set successfully.");
}
else
{
    Console.WriteLine("The request to set the record state failed.");
}

和IncidentState值列表:

IncidentStateSNC.NEW                = "1";

IncidentStateSNC.IN_PROGRESS        = "2";

IncidentStateSNC.ACTIVE             = IncidentStateSNC.IN_PROGRESS;

IncidentStateSNC.ON_HOLD            = "3";

IncidentStateSNC.AWAITING_PROBLEM   = IncidentStateSNC.ON_HOLD;

IncidentStateSNC.AWAITING_USER_INFO = IncidentStateSNC.ON_HOLD;

IncidentStateSNC.AWAITING_EVIDENCE  = IncidentStateSNC.ON_HOLD;

IncidentStateSNC.RESOLVED           = "6";

IncidentStateSNC.CLOSED             = "7";

IncidentStateSNC.CANCELED           = "8";

这里有一篇关于How to set the state on dynamic entity的好文章。

答案 1 :(得分:1)

  1. 您无法设置StateCode&amp;创建机会记录时的StatusCode,系统将默认设置为打开
  2. 类似错误说StatusCode 3不是Open的有效组合。 Read my SO answer
  3. SetStateRequest为deprecated。因此,一旦创建了商机,请使用适当的赢/输组合
  4. 执行更新请求