Domanda

Using this code I can set the state and status of a CRM entity in C#.

SetStateRequest appointmentRequest = new SetStateRequest
{
    State = new OptionSetValue(3), //Scheduled
    Status = new OptionSetValue(5), //Busy
    EntityMoniker = new EntityReference("appointment", myEntity.Id)
};

crmsvc.Execute(appointmentRequest);

A few lines before this I'm creating this same entity using

crmsvc.Create(myEntity);

Doing this in two requests seems dumb, so I went looking for a way to do it in one request.

Found this question I need to set the State and StatusCode of a custom entity and it seems that the statuscode can be set before saving, but I can't find any samples doing it with state as well.

Tried doing this, but it dosen't work

myEntity["state"] = new OptionSetValue(3);

It throws back:

Entity doesn't contain attribute with Name = 'state'. (Fault Detail is equal to Microsoft.Xrm.Sdk.OrganizationServiceFault).: 'Entity' entity doesn't contain attribute with Name = 'state'.

So can both state and statuscode be set before the first save?

È stato utile?

Soluzione

As you could see in the list of attributes the state code attribute is not valid for create. That means the CRM will simply ignore the value you are passing in.

Changing the state is always an explicit action. So you are not able to combine this into one single call.

However, you could define the statuscode already during the create call.

Also be aware that the attributes which expresses the state of a record are statecode and statuscode and their values depend on each other.

Altri suggerimenti

I don't see field State here http://msdn.microsoft.com/en-us/library/gg334225.aspx

We use statuscode and statecode, I don't think that this will work, but you can try.

Both statecode and statuscode are required to set the status of an entity through the CRM services. This is unintuitive since statecode can be derived from statuscode but it is the way the system works.

From a more abstract perspective there are two ways that you could go about doing a status change with only the statuscode:

1) Use the metadata service to dynamically retrieve the statuscode for your entity given the statecode at runtime. You could then cash the statecode values so that you only have to retrieve them once per entity per application scope.

2) Generate custom entity classes that include the entities statecode / statuscode pairings.

For either of the above options you can then create a set status method for your entities that derives the statecode from the statuscode that you pass it.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top