I suppose you are referring to BusinessFoundation MetaClass? My memory is rusty now, but it seems you would have to do like this
var field = metaClass.Fields["MyField"];
field.Attributes["MaxLength"] = 123;
field.AcceptChanges()
MetaField implements IChangeTracking and you should have to call .AcceptChanges to persit the changes
I must be going crazy, because I can't call .AcceptChanges() for some reason.. I get this compilation error:
'MetaField' does not contain a definition for 'AcceptChanges' and no extension method 'AcceptChanges' accepting a first argument of type 'MetaField' could be found (are you missing a using directive or an assembly reference?)
But this doesn't make any sense since, like you said, if I F12 onto MetaField it clearly implements IChangeTracking but somehow also lacks the AcceptChanges method.. I must be going crazy, doesn't seem to make any sense.
I've also tried casting as IChangeTracking to call AcceptChanges:
var field = metaClass.Fields["MyField"];
field.Attributes["MaxLength"] = 123;
((IChangeTracking)field).AcceptChanges();
This runs any errors but still doesn't seem to persist the change.
Confused :-/
Matt
Must be my bad, it might implement ITrackingChange explicitly so you need to cast it to ITrackingChange to call that method.
Not sure why it does not work, will get back to you later
Hi Quan
I don't think you're wrong, Inspecting the MetaField class it definitely does implement IChangeTracking as you said. It does implement the AcceptChanges but, again like you said, you have to cast to IChangeTracking to call the method.
But, for some reason, it's still not working for me :-/ Feel like I must be missinng something - just out of ideas now..
Thanks for helping - let me know if you think of anything else.
Matt
I even tried this:
var field = metaClass.Fields["MyField"];
field.Attributes["MaxLength"] = 123;
((IChangeTracking)field.Attributes).AcceptChanges();
((IChangeTracking)field).AcceptChanges();
((IChangeTracking)metaClass).AcceptChanges();
But still no luck
I have a little more time to look into this, and this probably the correct way to go
using (MetaClassManagerEditScope editScope = DataContext.Current.MetaModel.BeginEdit())
{
var field = metaClass.Fields["MyField"];
field.Attributes["MaxLength"] = 123;
editScope.SaveChanges();
}
Untested by me :)
Bingo!
Thanks Quan - have used this in the past but had completely forgotten about it! Now is working exactly as I need :)
Hi
I'm trying to programmatically change some attributes on a MetaField it doesn't persist. For example:
I don't get any errors from this code, but it doesn't persist the change.
I haven't found any special API's to do this or info on Google about if this is possible.
Thanks, Matt