Answered by:
Post-Operation Plugin not working

Question
-
I have a plugin that I am using to do a count.
I have registered the plugin as follows:
- Message: Create
- Primary entity: new_eval
- Stage of execution: Post-operation
- Execution mode: Synchronous
- Execution order: 1
When I run the plugin with stage of execution in pre-operation mode it executes fine except it is incrementing one behind i.e. instead of new_complextest being 2 it has value of 1.
It is like it is one behind in that it doesnt count the record being created.
When I run the plugin in Stage of Execution in Post-operation mode it doesnt update field new_complextest at all.
I have Here is my plugin code:
var complexQuery = (from e in mycontext.CreateQuery<new_eval>() where e.new_QW1 != null select e.new_QW1); int count = 0; foreach (var e in complexQuery) { count++; } entity.Attributes.Add("new_complextest", count);
Anyone know why this is happening, I thought Post-operation would cause the correct answer as it would increment count after the record was created?
Wednesday, October 28, 2015 2:18 PM
Answers
-
If it's in the post stage, you need to use a specific Update call to change the record. This is best done by creating a new instance of Entity and setting the Id property:
Entity entityToUpdate = new Entity(entity.LogicalName); entityToUpdate.Id = entity.Id; entityToUpdate.Attributes.Add("new_complextest", count); service.Update(entityToUpdate);
Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk
- Marked as answer by JMcCon Thursday, October 29, 2015 2:39 PM
Thursday, October 29, 2015 12:27 PMModerator
All replies
-
How are you instantiating and using your variable 'entity' ? If it's the Target InputParameter, then changes to attributes are only saved if the plugin runs in one of the Pre stages, and not the Post stage
Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk
Wednesday, October 28, 2015 4:26 PMModerator -
The entity is being instantiated using the "Target" How should I instantiate it so it can be used in the post stage?Wednesday, October 28, 2015 7:33 PM
-
If it's in the post stage, you need to use a specific Update call to change the record. This is best done by creating a new instance of Entity and setting the Id property:
Entity entityToUpdate = new Entity(entity.LogicalName); entityToUpdate.Id = entity.Id; entityToUpdate.Attributes.Add("new_complextest", count); service.Update(entityToUpdate);
Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk
- Marked as answer by JMcCon Thursday, October 29, 2015 2:39 PM
Thursday, October 29, 2015 12:27 PMModerator -
Thanks David.
I have another problem. I am trying to use a plugin to update a related entity with a value from one entity.
Would I need to use a workflow rather than a straight plugin?
Any ideas how to do this?
Thursday, October 29, 2015 2:40 PM