Community
Showing results for 
Search instead for 
Do you mean 
Reply

Get Field Length for Existing Field

Loyal Listener
Posts: 69
Country: United States

Get Field Length for Existing Field

Trying to get field lengths for existing fields.  Can't quite get the syntax right.  Can anyone point me in the right direction?

 

TIA

 

Mike Fortier

Fortier Consulting, LLC

mike@fortierconsulting.com

Loyal Listener
Posts: 69
Country: United States

Re: Get Field Length for Existing Field

Ok, I think I found it...

 

 Function GetFieldLength(ByVal ACTFM As ActFramework, ByVal strField As String, ByVal strtable As String) As Integer
        Dim rt As RecordType

        Select Case strtable
            Case "Company"
                rt = RecordType.Company
            Case "Contact"
                rt = RecordType.Contact
        End Select

        Dim fieldc As Database.FieldDescriptorCollection = ACTFM.Fields.GetFields(rt)
        Dim field As Database.FieldDescriptor = fieldc.Find(strField)
        GetFieldLength = field.GetSize
    End Function

Bronze Elite Contributor
Posts: 2,115
Country: United_Kingdom

Re: Get Field Length for Existing Field

Hi Mike,

 

The way I do this is by using the Act.Framework.Database.FieldDescriptor object thus:

 

field.Attributes[Act.Framework.Database.FieldProperty.Length] = new Act.Framework.Database.LengthAttribute(int.Parse(cell.Value.ToString()));

 

 Where cell is a DataGrideViewCell of a DataGridViewRow and field is a FieldDescriptor object.

 

HTH 

 

Vivek Gargav
Caldere Associates Ltd.
www.caldere.com
vgargav@caldere.com
My Blog
Loyal Listener
Posts: 69
Country: United States

Re: Get Field Length for Existing Field

Thanks, although it looks like this is code for setting a field length?

 

I've discovered that my earlier code solution was wrong, and the GetSize doesn't return the valid length.

 

Here is my second attempt:

 

Function GetFieldLength(ByVal ACTFM As ActFramework, ByVal strField As String, ByVal strtable As String) As Integer
        Dim rt As RecordType
        Dim fieldType As Integer
        Dim tryFL As act.Framework.Database.LengthAttribute 'act.Framework.Database.FieldDescriptorAttribute

        Select Case strtable
            Case "Company"
                rt = RecordType.Company
            Case "Contact"
                rt = RecordType.Contact
        End Select

        Dim fieldc As Database.FieldDescriptorCollection = ACTFM.Fields.GetFields(rt)
        Dim field As Database.FieldDescriptor = fieldc.Find(strField)

        fieldType = field.FieldDataType
        Select Case fieldType
            Case 12 'Date Time
                GetFieldLength = 8
            Case 17 'phone field type
                GetFieldLength = 32
            Case 16 'email field type
                GetFieldLength = 256
            Case Else
                tryFL = field.Attributes(act.Framework.Database.FieldProperty.Length)
                GetFieldLength = tryFL.Length
        End Select
    End Function

Bronze Elite Contributor
Posts: 2,115
Country: United_Kingdom

Re: Get Field Length for Existing Field

Hi mike,

 

Yeah it was for setting but you can easily  assign it to a FieldDescriptorAttribute and return it as a string or manipulate it an Integer. I' not by a VS machine at the mo so I haven't tested this.

 

 

Vivek Gargav
Caldere Associates Ltd.
www.caldere.com
vgargav@caldere.com
My Blog
Employee
Posts: 1,163
Country: USA

Re: Get Field Length for Existing Field

If your looking for the length of the field, I believe GetLength() will work. This value will vary depending on the field type and whether or not it is using a drop-downlist, but in general for char fields this will return the maximum number of chars you can have in the field.

 

However, if your looking for the length of the data that is in the field you can accomplish that like:

 

 

 

Contact c = afw.Contact.GetMyRecord();

string s = c.Fields["Contact", Act.Framework.MutableEntities.FieldNameType.Alias].ToString();

 

Now that you have the field data as a string object getting it's length is straightforward. 

 

 

Matthew Wood
Act! SDK Support
Community Moderator
Bronze Elite Contributor
Posts: 2,115
Country: United_Kingdom

Re: Get Field Length for Existing Field

Doh!

Now why didn't I see that handy little method?! It would have saved me going through contortions!

Vivek Gargav
Caldere Associates Ltd.
www.caldere.com
vgargav@caldere.com
My Blog