0

I'd like to change a value of field_1 in case the user changed the field_2. Normally, one does that on the client using JS but in this case, the user might change his mind again, back to the original value. So, I'd like to update a field in a plugin.

I'm successfully discovering the change in field_2 but when I assign a value to field_1, it doesn't get stored in the database. What do I do wrong?

if (IsField_2Changed())
   (Context.InputParameters["Target"] as Entity).Attributes["field_1"] = 666;

I get no crashes, no complaints but the value of 666 (because it's Halloween) does't get stored to the database. The code is executed on the message of update.

Please note that the user doesn't change field_1. It's going to be set as a read-only field later on.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
  • Possible duplicate of [CRM 2011 KeyNotFoundException exception](http://stackoverflow.com/questions/9901527/crm-2011-keynotfoundexception-exception). – Peter Majeed Oct 29 '12 at 23:16
  • @PeterMajeed Not really, Peter. Not this time, hihi. I don't get any errors at all. The field **is** found (despite my problems with not finding stuff the last few days). It's written to as well. **However**, the value that I stick into the target entity doesn't get stored to the database. – Konrad Viltersten Oct 30 '12 at 00:41

2 Answers2

1

This is a bit of guess as you don't mention how your plugin is registered.

However I'm guessing the plugin is registered on the Post when it should be on the Pre.

If you want to update by using the Target you need to do it in the Pre.

If your plugin has to be on the Post, use a webservice update call.

James Wood
  • 17,286
  • 4
  • 46
  • 89
  • What exactly do you mean by "*using a webservice update call*"? Since I prefer not to put my plugin on pre-state (of fear for what the rest of the code would do if I change my basic assumption on when it gets executed) I've resolved the issue by simply re-updating the entity. Of course, one has to be careful not to cause recurrence (I know that for a fact, a very painful fact) but other than that, it seems to work. Then I got thinking - **maybe James had an even better idea**, so I'm asking straight out. – Konrad Viltersten Nov 03 '12 at 11:40
  • By "using a webservice update call" I meant issuing a SOAP or REST request, e.g. `service.Update(entity)`. Which is probably what you are doing already. If I have understood this correctly: "I've resolved the issue by simply re-updating the entity". Plugins repeatedly triggering themselves is just something that has to be handled, usually by checking the `IExecutionContext.Depth` property: http://msdn.microsoft.com/en-us/library/microsoft.xrm.sdk.iexecutioncontext.depth.aspx – James Wood Nov 04 '12 at 21:11
  • Cool. I didn't know about `IExecutionContext.Depth`. I'll try that the next time. – Konrad Viltersten Nov 05 '12 at 00:41
0

You can do this inside JavaScript. Just add your function to the OnChange event of field2. It is not an OnBlur event, so it will only fire when the user leaves the field after changing.

If you need to fire every time the user leaves the field, you can also bind to the OnBlur event via JavaScript (not with the fields on change event), but it's unsupported.

For the plugin though, here's a nice walk through (although the logic isn't exactly the same) http://nzcrmguy.blogspot.com/2011/03/updating-fields-on-crm-2011-plugin.html

Paul Way
  • 1,966
  • 1
  • 13
  • 10
  • Naa, JS won't do in this case. I need to solve it using a plugin. I'll take a look at the walk-though but I'm having a suspicion that I got it right just except for some minor detail. And **boy** that one one sloppy and ugly formatting on that blog. :) – Konrad Viltersten Oct 30 '12 at 04:16