Can I create an inactive record by plugin in CRM 2011?
Asked Answered
F

2

5

Is there way to create an inactive record by plugin in Microsoft CRM 2011? I can use setStateRequest class and IOrganizationService.Execute method to change state/status of an existing record. Since this method needs the record Id, It can't be use on a new record.

Is it possible to create immediately an inactive record or do I have to create a new active record and deactivate it after the creation?

Setting the state/status directly (even on a new record)

EntityToSave["statecode"] = new OptionSetValue(state);
EntityToSave["statuscode"] = new OptionSetValue(status);

leads to an error:

2 is not a valid status code for state code ProductState.Active on product
Feaze answered 18/7, 2013 at 11:46 Comment(0)
D
8

You need to create the record first and after deactivate with a SetStateRequest.

There are no other ways to proceed.

Dashed answered 18/7, 2013 at 11:51 Comment(6)
The SetStateRequest is no longer needed in CRM 2015 Update 1 and going forward.Methodize
@Daryl: and what is needed to be done with CRM 2015? Thank you!Oscaroscillate
@Oscaroscillate just update the status and state code as neededMethodize
@Daryl: Thank you for your quick reply. However for the status we have optionset values visible from CRM which is not the case for state, so do I consider the 0 value for Active and 1 for Inactive? Thank youOscaroscillate
@Oscaroscillate generally yes. I use the Early Bound Entities to help with that. You can also search for the DLaB.Xrm NuGet Package. It contains ActivePropertyInfo and LateBoundActivePropertyInfo classes that will give the ActiveState or NotActive State for the Entity.Methodize
To expand on @Methodize comment; SetStateRequest is deprecated and shouldn't be used in CRM 2015 Update 1 and higher. However you still have to Create the record in an active state and execute an Update to set the state to inactive.Soldier
M
2

I would think you should be able to register a plugin that runs during the Post-operation step on the create message, that updates the entity to inactive using the SetStateRequest. As long as it is the first plugin to fire, everything else in the system is going to see it as being created in an inactive state.

This will cause any SetStateDynamicEntity plugins to execute, but you can use plugin execution variables to handle that as well.

Update

Since CRM 2015 U1, you shouldn't be using the SetStateRequest. Just use a standard update statement inside the Post Create.

Methodize answered 18/7, 2013 at 13:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.