Reputation: 21
In a Plugin i have to update the record the plugin was fired by on pre sync, post sync, post async.
In Pre-sync-state, i just have to update the Context-Entity e.g. mycontact.Attributes["lastname"] = "ABC" and the changed value is automatically saved.
In Post-Update, i can not do that as its already in the Database (but not committed). My attempts to use the service (service.update(mycontact) failed.
What is the best way to Update a record in Post-Update (sync/async) - if its possible at al?!
Updating other Records is no problem, but updating record the plugin was fired by / from does not work. :-(
Any Ideas?
Regards, Nick
Upvotes: 2
Views: 9184
Reputation: 38
In Pre :-
Try Updating the context as as it saves the data of the context:-
Entity test= (Entity)context.InputParameter["Target"];
test["name"]="ABC";
context.InputParaeter["Target"] = test;
In Post(Syn/Asyn):-
Try Creating a new Object for Updating the record Like
Entity test=new Entity("contact");
test.Id=((Entity)context.InputParameter["Target"]).Id;
test.Attributes["firstname"]="ABC";
service.Update(test);
And Also Please Check
Context.Depth<=1;
Upvotes: 1
Reputation: 31
You can update the record either in Pre Sync, Post Sync plugin or Post Async plugin.
To update the record in Pre Sync plugin, you just need to add field value in the Context Entity as below:
Entity e = context.InputParameters["Target"] as Entity;
e["attribute_name"] = somevalue;
To update the record in Post Sync plugin, you need to add field value in the context entity and call Update method of the IOrganizationService as below:
Entity e = context.InputParameters["Target"] as Entity;
e["attribute_name"] = somevalue;
service.Update(e);
To update the record in Post Async plugin, you need to do the same thing as done for Post Sync plugin.
'Pre Sync' plugin is recommended if you just want to update the field of the same record. This will reduce an update operation as this step executes before Core operation stage.
If you want to set the same record as EntityReference (Lookup) to another record, then you will need to do this operation in Post Async plugin.
Upvotes: 3
Reputation: 1601
The
service.Update(entity)
is supposed to work in the post stage. Anyway during the update operation you do not have access to all the fields of the entity, but only to the ones that have been updated. If you want to modify other fields you need to retrieve the entity by using a retrieve (always retrieve only the fields you need for better performances). Essentially the problem should be that you are reading a value, or updating a field that has not been forwarded to the context and that is therefore null.
Upvotes: 0
Reputation: 2123
You have to retrieve the record you are updating
Entity EntityUpdate= service.retrieve(entity,id,true);
then update the fields like
EntityUpdate["field1"] = "abc";
then
service.update(EntityUpdate);
Upvotes: 0
Reputation: 34
You are right, after the record is saved to the database you will not be able to update the context record by directly modifying its contents.
Instead, you need to create a new entity object and then update it with the new values.
Entity oContact = New Entity("contact");
oContact.Id = myContact.Id;
oContact.Attributes.Add("lastname", "ABC");
organizationService.Update(oContact);
Upvotes: -1
Reputation: 21
As previously stated by others when calling an update on the current record post operation you will have to trap for the context.depth property being greater than 1.
I can strongly advise you that if you are editing the current record only and don't need to associate other entities to it then register your plugin pre-operation. That way you simply alter the attribute(s) and let the main operation commit them to the database.
Upvotes: 1
Reputation: 31
In the above case the sync operation will not work as the values are not saved yet in the DB so the option left is async-post-update with the check of
if (context.Depth > 1){ return; } //To Avoid Multiple Trigger Of Same Update Plugin.
Upvotes: 3
Reputation: 33
You can use a method to avoid the plugin to run more than once using this.
if (localContext.PluginExecutionContext.Depth > 1) { return; }
Hope thats what you mean.
Upvotes: 1
Reputation: 1888
My guess is that your plugin is being triggered multiple times and hitting the recursive limit on plugins.
As a rule, you only want to have the plugin triggered when the relevant fields are altered.
Also, make sure that your update call only has the attributes that need to be updated on the object that your are sending to the update method. Most of the time, I will not update the object from the Plugin Context, but I would update a new record with the Id property set. This helps prevent recursive scenarios.
You will want to add more information to your question if you want a more specific answer.
Upvotes: 0