Community
Showing results for 
Search instead for 
Do you mean 
Reply

Changing datatype of an existing field ( and length )

Copper Elite Contributor
Posts: 95
Country: USA

Changing datatype of an existing field ( and length )

Two part question, first, if I have a field that is say, a character field, how do I change it's type to decimal using the SDK, and have all the data convert (at least all the data that would be a valid decimal).

Second part, If I have a character field with a length of 25, how do I change it so that it has a length of 50?
Nick Bohne
Effective Solutions, Inc.
Bronze Elite Contributor
Posts: 2,115
Country: United_Kingdom

Re: Changing datatype of an existing field ( and length )

Hi Nikolas,
 
Just off the top of my head I think you could use the FieldDescriptorManager.Convert(oFieldDescriptor,FieldDataType); method. The FieldDataType is an Enumeration of the Act.Framework.Database class.
 
Convert String to Decimal:
decimal decVal = System.Convert.ToDecimal(strVal);
 
Quick rough and ready way....
You could iterate through the {Entity}List of !null criteria of the field in question and capture the Contactid and fieldVal into a 2D Array.
Carryout out the convert on the FieldDescriptor and then double iterrate through the 2D array and repopulate the field using the static convert method above.
 
I haven't tried any of this yet but will check when I get into the office tomorrw afternoon.
If I've made any silly and glaring mistakes anyone else feel free to pipe in and correct me! Smiley Very Happy
 
Hope that gives you some ideas and goes some way to help.
 
Vivek
Vivek Gargav
Caldere Associates Ltd.
www.caldere.com
vgargav@caldere.com
My Blog
Copper Elite Contributor
Posts: 95
Country: USA

Re: Changing datatype of an existing field ( and length )

Yeah when I try to change the type it doesn't throw any exceptions, but the type also doesn't change (even when there is no data in the field)
Nick Bohne
Effective Solutions, Inc.
Bronze Elite Contributor
Posts: 2,115
Country: United_Kingdom

Re: Changing datatype of an existing field ( and length )

Hiya Nik,
 
This is the code Ijust tried on the ActDemo dB and it worked fine for me. What I suspect that you might not have done was call the Save method of the FieldDescriptorManager after locking and converting the field in question.
N.B. Util is just a static class holding the ActFramework instance.
 
 

Act.Framework.Database.FieldDescriptorCollection oFields = Util.oFramework.Fields.GetFields(Act.Framework.RecordType.Contact);

Act.Framework.Database.FieldDescriptor oField = oFields.Find("User 1");

Util.oFramework.Database.LockDatabase(Act.Framework.DatabaseLockReason.SchemaChanges);

Util.oFramework.Fields.Convert(oField, Act.Framework.Database.FieldDataType.Decimal);

Util.oFramework.Fields.Save(oField);

MessageBox.Show(oField.FieldDataType.ToString());

 
HTH
Vivek
 
Vivek Gargav
Caldere Associates Ltd.
www.caldere.com
vgargav@caldere.com
My Blog
Copper Elite Contributor
Posts: 95
Country: USA

Re: Changing datatype of an existing field ( and length )

Heh I should have been more specific, for me it fails on custom entity fields, contact fields works fine =)
Nick Bohne
Effective Solutions, Inc.