我想在CRM中创建“中奖机会”,但出现以下错误。

错误:


  附加信息: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);
    }

最佳答案

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

要获得通过,您需要使用SetStateRequest

您可以通过msdn示例初始化SetStateRequest的新类并进行设置:

// 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的不错的文章。

关于c# - 无法在CRM中创建记录,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48071330/

10-13 06:05