locked
Error setting State on create in plugin RRS feed

  • Question

  • I'm trying to set the state of an object using a post stage plugin on the parent pipeline for a custom object based on some criteria of the object's data.

    Here's the section that sets the state

    if (accomplished)
    {
    	entity.Properties.Add(new StatusProperty("statuscode", new Status(3)));
    	entity.Properties.Add(new StateProperty("statecode","inactive"));
    }
    else
    {
    	entity.Properties.Add(new StatusProperty("statuscode", new Status(1)));
    	entity.Properties.Add(new StateProperty("statecode", "active"));
    }
    
    But it gives me an "Invalid Argument" error. But I can't seem to get any more information then that. I've enabled tracing which doesn't show any information related to it.

    Another method I've tried is in the post stage setting the status with a separate execute call 

    if (context.Stage == MessageProcessingStage.AfterMainOperationOutsideTransaction)
    {
    int linenumber = 0;
    try
    {
    	bool update = false;
    	ICrmService crmService = context.CreateCrmService(true);
    	SetStateDynamicEntityRequest stateReq = new SetStateDynamicEntityRequest();
    
    	stateReq.Entity = new Moniker();
    
    	stateReq.Entity.Id = DynamicObjects.GetPropertyValue<Guid>(entity, entity.Name.ToLower() + "id");
    	stateReq.Entity.Name = entity.Name;
    	if (accomplished)
    	{
    		//Set to Inactive
    		stateReq.State = "inactive";
    		stateReq.Status = 3;
    	}
    	else
    	{
    		//Set to Inactive
    		stateReq.State = "active";
    		stateReq.Status = -1;
    	}
    	crmService.Execute(stateReq);
    }
    catch (System.Web.Services.Protocols.SoapException ex)
    {
    	throw new InvalidPluginExecutionException(
    			String.Format("An error occurred in the {0} plug-in.", this.GetType().ToString()), ex
    	);
    }
    catch (Exception)
    {
    	throw new Exception(DynamicObjects.GetPropertyValue<Guid>(entity, entity.Name.ToLower() + "id") + linenumber.ToString() + " " + accomplished);
    }
    }
    

    But that just errors because the GUID is not set yet
    Monday, February 15, 2010 6:20 PM

Answers

  • Turns out I had to do the following.

    							
    if (context.MessageName.Equals(MessageName.Create.ToString()))
    	stateReq.Entity.Id = (Guid)context.OutputParameters["id"];
    else
    	stateReq.Entity.Id = DynamicObjects.GetPropertyValue<Guid>(entity, entity.Name.ToLower() + "id");

    Seems kind of silly to have to do that to me but it works.
    Monday, February 15, 2010 6:51 PM

All replies

  • using SetStateDynamicEntityRequest  is the correct method, but you need to set the EntityId, is EntityId you are setting correct for the entityname you are specifying?

    http://blogs.msdn.com/jonasd/archive/2007/04/04/setting-status-with-setstatedynamicentityrequest.aspx
    http://blog.cybner.com.au/2008/07/set-statestatus-of-custom-entity-in-crm.html
    http://www.ureader.com/message/33230890.aspx
    Monday, February 15, 2010 6:29 PM
  • stateReq.Entity.Id = DynamicObjects.GetPropertyValue<Guid>(entity, entity.Name.ToLower() + "id");

    has a value of all 0's so the call to execute fails.
    Monday, February 15, 2010 6:32 PM
  • Hi paulo, it should not be zero, you need to set the Guid of the entity otherwise the call will faill.
    if you are registering this on postCreate event, you can get the guid like this.
    Guid entityId = new Guid(context.OutPutParameters["id"].ToString());

    remember there is no id on Pre-Create plugin.

    if it is pre/post update then get it from teh entit primarkey

    DynamicEntity  d = (DynamicEntity)context.InputParemeters[ParemeterName.Target];
    Guid entityId = ((Key)d.Properties["yourprimarykeyattribute"]).Value;

    Monday, February 15, 2010 6:36 PM
  • Turns out I had to do the following.

    							
    if (context.MessageName.Equals(MessageName.Create.ToString()))
    	stateReq.Entity.Id = (Guid)context.OutputParameters["id"];
    else
    	stateReq.Entity.Id = DynamicObjects.GetPropertyValue<Guid>(entity, entity.Name.ToLower() + "id");

    Seems kind of silly to have to do that to me but it works.
    Monday, February 15, 2010 6:51 PM