我需要设置一个自定义实体的国家和的StatusCode(I need to set the Stat

2019-08-01 02:32发布

我需要设置使用.net CRM SDK的自定义实体的国家的StatusCode。
下面的代码执行,但是当我检查的实体形式的的StatusCode没有改变。

private void SetState(Entity entity, int statuscode)
{
  SetStateRequest setState = new SetStateRequest
  {
    EntityMoniker = new EntityReference(
      entity.LogicalName, new Guid(entity.Id.ToString())),
    State = new OptionSetValue(0),
    Status = new OptionSetValue(statuscode)
  };
  SetStateResponse myres = (SetStateResponse)svc.Execute(setState);
}

Answer 1:

你可以试试下面的代码,我用这个代码来设置状态。

Microsoft.Xrm.Sdk.EntityReference moniker = new EntityReference();
moniker.LogicalName = "contract";
moniker.Id = newContractId;

Microsoft.Xrm.Sdk.OrganizationRequest request 
  = new Microsoft.Xrm.Sdk.OrganizationRequest() { RequestName = "SetState" };
request["EntityMoniker"] = moniker;
OptionSetValue state = new OptionSetValue(1);
OptionSetValue status = new OptionSetValue(2);
request["State"] = state;
request["Status"] = status;

_service.Execute(request);

或者你也可以设置这样的状况:

int statusCode = 123456;
entity["statuscode"] = new OptionSetValue(statusCode);
_service.Update(entity);


文章来源: I need to set the State and StatusCode of a custom entity