function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Is there an Apex example that shows how to create a custom text field?

Is there an Apex example that shows how to create a custom text field?


The one given in the vb sample gives an error 

Can not specify 'precision​' for CustomFiel​d of text Type


No precision is specified.


You need to put that in a bit more context. I assume you mean you're trying to create a custom field using the Metadata API ? Not many people on these forums will be writing Java or .Net apps to do this as it's so simple to configure in the platform or existing tools.


I am running the VB sample code but it does not work. It errors out saying you cannot set precision for a text field but precision is not set. I was looking for working example.


Private Sub createCustomFieldSample()
        'Verify that we are already authenticated, if not
        'call the login function to do so
        If Not loggedIn Then
            If Not login() Then
            End If
        End If
            Console.WriteLine(vbCrLf & "This sample will create a new Text field on the Lead object.")
            Console.Write("Enter the name that you would like for the field: ")
            Dim fieldName As String = Console.ReadLine()
            If fieldName.Length = 0 Then
                Console.Write("No name was entered for the field, so we are exiting this sample.")
            End If
            Dim cf As CustomField = New CustomField()
            cf.description = "Simple text field from API"
            cf.fullName = "Lead." & fieldName & "__c"
            Console.Write("Enter a label for the field: ")
            Dim fieldLabel As String = Console.ReadLine()
            If fieldLabel.Length = 0 Then
                fieldLabel = "Sample Field"
            End If
            cf.label = fieldLabel
            cf.length = 50
            cf.type = apexMetadata.FieldType.Text

            Dim mBinding As MetadataService = New MetadataService()
            mBinding.SessionHeaderValue = New apexMetadata.SessionHeader()
            mBinding.SessionHeaderValue.sessionId = binding.SessionHeaderValue.sessionId
            mBinding.Url = binding.Url.Replace("/u/", "/m/")

            Dim asyncResult As AsyncResult = mBinding.create(New Metadata() {cf})(0)

            Do While asyncResult.state = AsyncRequestState.InProgress

                Thread.Sleep(asyncResult.secondsToWait * 1000)
                Console.WriteLine("Checking status..." & vbCrLf & vbTab & "State:" & asyncResult.state & vbCrLf & vbTab & "Status: " & asyncResult.statusCode & vbCrLf & vbTab & "Seconds to wait: " & asyncResult.secondsToWait)
                asyncResult = mBinding.checkStatus(New String() {})(0)
            If asyncResult.state = AsyncRequestState.Completed Then
                Console.Write("Custom object has been created. " & vbCrLf & vbCrLf & "Use the describe sobject sample to see your object is not listed.")
            End If

        Catch ex As Exception
            Console.WriteLine("\nFailed to create object: " & vbCrLf & ex.Message)
            Console.WriteLine("\nHit return to continue...")
        End Try
    End Sub