November Happy Hour will be moved to Thursday December 5th.

Programmatically updating MetaField attributes

Vote:
 

Hi

I'm trying to programmatically change some attributes on a MetaField it doesn't persist. For example:

metaClass.Fields["MyField"].Attributes["MaxLength"] = 123;

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

#188224
Feb 15, 2018 8:03
Vote:
 

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 

#188227
Feb 15, 2018 9:29
Vote:
 

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

#188236
Feb 15, 2018 11:42
Vote:
 

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 

#188237
Feb 15, 2018 12:06
Vote:
 

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

#188238
Feb 15, 2018 12:22
Vote:
 

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

#188242
Feb 15, 2018 12:32
Vote:
 

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 :) 

#188311
Feb 19, 2018 14:50
huan duy - Dec 11, 2022 8:19
As I see, your below code only applies to case meta field is OnBusinessFoundationObject, what if I want to update the meta fields of OrderContext ( eg: OrderContext.Current.LineItemMetaClass). Is EPI support that?
Vote:
 

Bingo!

Thanks Quan - have used this in the past but had completely forgotten about it! Now is working exactly as I need :)

#188323
Feb 19, 2018 23:33
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.