简体   繁体   中英

How to make an entity update its own field

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM