【问题标题】:Not able to create record in CRM无法在 CRM 中创建记录
【发布时间】:2018-01-24 12:56:41
【问题描述】:

我想在 CRM 中创建 Won Opportunity,但出现以下错误。

错误:

附加信息:3 不是状态码的有效状态码 OpportunityState.打开带有 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);
    }

【问题讨论】:

标签: c# dynamics-crm microsoft-dynamics


【解决方案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 的好文章。

【讨论】:

  • 谢谢 我明白我必须做什么。错误已解决。
  • 这适用于大多数记录类型,但不是机会。您需要使用特定的 WonOpportunity/LostOpportunity 消息来设置机会的状态。
  • @vidhi 你是怎么解决的?
【解决方案2】:
  1. 创建机会记录时不能设置 StateCode 和 StatusCode,系统将默认设置为 Open
  2. 像错误一样,StatusCode 3 不是 Open 的有效组合。 Read my SO answer
  3. SetStateRequest 是deprecated。因此,一旦创建机会,请使用适当的赢/输组合发出更新请求

【讨论】:

    猜你喜欢
    • 2016-09-25
    • 1970-01-01
    • 1970-01-01
    • 2014-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多