我想在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);
}
答案 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)