Answered by:
What is the use of SetState Message in Plug-in Registration

Question
-
Hi,
What is the use of SetState SetStateDynamic Messages in Plug-in Registration ?
Can you please provide me the examples on them??
Thanks,
Srikanth Reddy
Friday, September 9, 2011 12:56 PM
Answers
-
Hi,
You will use SetStateDynamicEntity message in plugin registraiton if you have to execute business logic on entity status change i.e. You have to perform some validations on custom or system entity status change to Deactivate then you will write the plugin register it on SetStateDynamicEntity and perform required validaitons in code and if validations failed then can throw error in InvalidPluignExecution exception.
i.e. Controlling Activate Quote Pirveleges:
As there is no standard feature available to control so the best way to control Quote Activation priveleges is via registering a plugin on SetStateDynamicEntity Message for Quote entity in Pre-Operaiton stage, the plugin code should check if Quote is activated and specified user or user with specified Security Role or having entity privelege is activating quote then allow execution else abort execution and throw exception, in plugin your code will be i.e.
// Check if Quote Entity is activated
if (context.InputParameters.Contains("EntityMoniker") && context.InputParameters["EntityMoniker"] is EntityReference &&
((EntityReference)context.InputParameters["EntityMoniker"]).LogicalName == "quote") {
switch (context.MessageName)
{// Check if entity status is changed
case "SetStateDynamicEntity":#region Configuraiton Activation Safe Check
if (context.InputParameters.Contains("State"))
{// Check if User has Activated the Quote
if (((OptionSetValue)context.InputParameters["State"]).Value == 1)
{
// Check if current user does not have Privillages then throw exceptionthrow new InvalidPluginExecutionException("Not enough privelegs to activate quote.");
}
}
}
Jehanzeb Javeed
http://worldofdynamics.blogspot.com
Linked-In Profile |CodePlex Profile
If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".- Edited by Jehanzeb.Javeed Friday, September 9, 2011 1:03 PM
- Proposed as answer by Jehanzeb.Javeed Friday, September 9, 2011 1:03 PM
- Marked as answer by DavidJennawayMVP, Moderator Wednesday, October 5, 2011 4:39 PM
Friday, September 9, 2011 1:03 PM
All replies
-
Hi,
You will use SetStateDynamicEntity message in plugin registraiton if you have to execute business logic on entity status change i.e. You have to perform some validations on custom or system entity status change to Deactivate then you will write the plugin register it on SetStateDynamicEntity and perform required validaitons in code and if validations failed then can throw error in InvalidPluignExecution exception.
i.e. Controlling Activate Quote Pirveleges:
As there is no standard feature available to control so the best way to control Quote Activation priveleges is via registering a plugin on SetStateDynamicEntity Message for Quote entity in Pre-Operaiton stage, the plugin code should check if Quote is activated and specified user or user with specified Security Role or having entity privelege is activating quote then allow execution else abort execution and throw exception, in plugin your code will be i.e.
// Check if Quote Entity is activated
if (context.InputParameters.Contains("EntityMoniker") && context.InputParameters["EntityMoniker"] is EntityReference &&
((EntityReference)context.InputParameters["EntityMoniker"]).LogicalName == "quote") {
switch (context.MessageName)
{// Check if entity status is changed
case "SetStateDynamicEntity":#region Configuraiton Activation Safe Check
if (context.InputParameters.Contains("State"))
{// Check if User has Activated the Quote
if (((OptionSetValue)context.InputParameters["State"]).Value == 1)
{
// Check if current user does not have Privillages then throw exceptionthrow new InvalidPluginExecutionException("Not enough privelegs to activate quote.");
}
}
}
Jehanzeb Javeed
http://worldofdynamics.blogspot.com
Linked-In Profile |CodePlex Profile
If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".- Edited by Jehanzeb.Javeed Friday, September 9, 2011 1:03 PM
- Proposed as answer by Jehanzeb.Javeed Friday, September 9, 2011 1:03 PM
- Marked as answer by DavidJennawayMVP, Moderator Wednesday, October 5, 2011 4:39 PM
Friday, September 9, 2011 1:03 PM -
These message are triggered when you change the state of the record for e.g diabled the record or enabled the record.
it is different in activites say open or completed or cancelled etc
For e.g
- when you disable the parent entity yo want to disable the child entities too.
- check some values are filled before you save the activity as completed.
Amreek singh Senior CRM Consultant CDC Praxa Sydney,Australia http://mscrmshop.blogspot.com/- Proposed as answer by Amreek Singh Friday, September 9, 2011 1:12 PM
Friday, September 9, 2011 1:12 PM -
You have to register for both SetState and SetStateDynamic Messages. From the MSDN Blog (http://blogs.msdn.com/b/crm/archive/2008/03/07/developing-plug-ins-for-crm-4-0.aspx)
2. Why do I need to register on SetState and SetStateDynamicEntity separately?
· As I mentioned earlier there are multiple messages that perform the same action in CRM. One such example is SetStateRequest and SetStateDyanmicEntityRequest . If you want to write a plug-in on SetState, then you need to register it on both messages.
Blake Scarlavai - http://mscrmdev.blogspot.com/ - Sonoma Partners - http://www.sonomapartners.com/- Proposed as answer by Blake ScarlavaiMVP Friday, September 9, 2011 1:21 PM
Friday, September 9, 2011 1:21 PM