0

I was trying to set a contract state from Active to Draft using the code below:

SetStateRequest setState = new SetStateRequest();
setState.EntityMoniker = new EntityReference();
setState.EntityMoniker.Id = contractId;
setState.EntityMoniker.Name = "Contract";
setState.EntityMoniker.LogicalName = "contract";
setState.State = new OptionSetValue(0);
setState.Status = new OptionSetValue(1);
SetStateResponse setStateResponse = (SetStateResponse)service.Execute(setState);

My aim is to update an active contract, but to do that I need to have it in Draft state to update it and then put it back to Active, when updated. I would really much appreciate if somone could help me out here.

The plugin right now is firing the following error: "The target state is invalid. The target state may not exist or the system does not allow changing to the target state from the current state. Please check the documentation on this state change request"

A Robben
  • 299
  • 2
  • 5
  • 20

1 Answers1

0

EDIT: It seems you must invoice a contract before you can cancel it - maybe it is not possible to set a contract status to draft from certain statuses? See MSDN example here:

http://msdn.microsoft.com/en-us/library/gg328503.aspx

If you only care about the state (rather than the status), you can pass '-1' as an option set value, which will be ignored when the request is executed. I have done something very similar in order to deactivate a product record.

/// <summary>
/// Deactivate the specified entity.
/// </summary>
/// <param name="entityRef">Entity to deactivate.</param>
/// <param name="service">Organisation service.</param>
/// <param name="status">Optional status code.</param>
private static void DeactivateEntity(EntityReference entityRef, IOrganizationService service, int status = -1)
{
    service.Execute(new SetStateRequest
        {
            EntityMoniker = entityRef,
            State = new OptionSetValue(1),
            Status = new OptionSetValue(status)
        });
}
Alec
  • 946
  • 1
  • 11
  • 22
  • Hi Alec thanks for your reply, but would that allow me to update my contract ? and furthermore in your code State is 1 (which on contract is invoice) so again here you cant't update a contract...Can you update entities while deactivated? – A Robben Jan 23 '13 at 16:41
  • You should be able to run updates against a deactivated entity, yes. This code was written for products, whose codes are 0 for Active, 1 for Inactive, which is not the same as Contract as you've correctly said; you'd need to change that in the code I gave you, but otherwise it ought to work. – Alec Jan 23 '13 at 16:51
  • Thanks, Well my problem will still be existent because contracts can not be deactivted... – A Robben Jan 23 '13 at 16:58