Reputation: 1993
I am writing a plugin which will essentially perform the same function - say, create a task when the status changes. The functionality needs to happen on 2 entities.
Every step is exactly the same except setting of the Entity Type field (an Option Set). This is set to EntityA
or EntityB
depending upon which entity triggered the plugin.
My existing code does the following
new_entitya entityA = (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity && context.PrimaryEntityName == "new_entitya")
? ((Entity)context.InputParameters["Target"]).ToEntity<new_entitya>()
: null;
Now, is there a way that I can set the value of .ToEntity call based on the value of PrimaryEntityName instead of writing new_entitya or new_entityb?
Upvotes: 1
Views: 1504
Reputation: 6715
Why cast at all? You can just check the entity name and apply whichever property you need to.
var newTask = new Entity("Task");
newTask.Attributes.Add("subject", "foo");
// etc etc for other common properties
if (context.PrimaryEntityName.Equals("new_entitya"))
{
newTask.Attributes.Add("new_optionset", valueA);
}
else
{
newTask.Attributes.Add("new_optionset", valueB);
}
I guess the downside is that you have to maintain the optionset values in the plugin but doesn't seem a huge overhead if it is just two values.
Upvotes: 2