C# 无法在CRM中创建记录

C# 无法在CRM中创建记录,c#,dynamics-crm,microsoft-dynamics,C#,Dynamics Crm,Microsoft Dynamics,我想在CRM中创建一个赢得的机会,但我遇到以下错误 错误: 其他信息:3不是状态代码的有效状态代码 OpportunityState。打开Id为的opportunity 8e99128b-3ef0-e711-8145-e0071b6641f1 代码: 在CRM中,状态代码为只读。添加记录时不能动态设置,因此问题中会出现错误 要通过它,你需要使用 您可以初始化SetStateRequest的一个新类,并根据msdn示例进行设置: // Create the Request Object SetSt

我想在CRM中创建一个赢得的机会,但我遇到以下错误

错误:

其他信息:3不是状态代码的有效状态代码 OpportunityState。打开Id为的opportunity 8e99128b-3ef0-e711-8145-e0071b6641f1

代码:


在CRM中,状态代码为只读。添加记录时不能动态设置,因此问题中会出现错误

要通过它,你需要使用

您可以初始化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.");
}
这里也有一篇很好的文章

  • 创建opportunity记录时不能设置StateCode和StatusCode,系统将默认设置为Open
  • Like error表示状态代码3不是Open的有效组合
  • SetStateRequest是。因此,一旦创建了opportunity,使用适当的赢/输组合执行更新请求

  • 谢谢,我明白我该做什么了。已解决错误。这适用于大多数记录类型,但不适用于opportunity。您需要使用特定的WonOpportunity/LostOpportunity消息来设置opportunity的状态。@vidhi您是如何修复的?
    // 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.");
    }
    
    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";