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

how to create custom field in standard object?



 i m trying to create a custom field in to standard object  but it is creating in to standard fields  not in custom field.

 and second thing i m not able to create field type  exept Text and AutoNumber.   this is the code that i m using.



// create a new custom object
                String objectName = "Test4";
                String displayName = "Test4 Object";
                CustomObject co = new CustomObject();
                co.setDescription("Created by gaurav using the Metadata API");
                // create the text id field
                String custom ="Test";
                CustomField field = new CustomField();
                field.setDescription("The Gaurav");
                // add the field to the custom object

 try {
                  // submit the custom object to salesforce
                  AsyncResult[] ars = metadataConnection.create(new CustomObject[] { co });
                  if (ars == null) {
                      System.out.println("The object was not created successfully");
                  String createdObjectId = ars[0].getId();
                  String[] ids = new String[] {createdObjectId};
                  boolean done = false;
                  long waitTimeMilliSecs = 1000;
                  AsyncResult[] arsStatus = null;
               * After the create() call completes, we must poll the results
               * of the checkStatus() call until it indicates that the create
               * operation is completed.
                  while (!done) {
                      arsStatus = metadataConnection.checkStatus(ids);
                      if (arsStatus == null) {
                          System.out.println("The object status cannot be retrieved");
                      done = arsStatus[0].isDone();
                      if (arsStatus[0].getStatusCode() != null )  {
                          System.out.println("Error status code: "+arsStatus[0].getStatusCode());
                          System.out.println("Error message: "+arsStatus[0].getMessage());
                      // double the wait time for the next iteration
                      waitTimeMilliSecs *= 2;
                      System.out.println("The object state is "+arsStatus[0].getState());
              System.out.println("The ID for the created object is "+arsStatus[0].getId());
                catch (Exception ex) {
                    System.out.println("\nFailed to create object, error message was: \n" +ex.getMessage());



Hi ,


I guess the above field addition is used for adding the mandatory field while creating custom object .


But In case If I want to add a new field for an existing object or during the creation of object if I want to add new custom fields how to do that?


hi, please suggest  i am also facing same  issue . i want to create a custome field  " add_description "with  standerd  lead object  by useing partner or metadata api in java 



Here you are setting the name field for the custom object and not creating a custom field.  That's why it's not allowing you to choose any type other than text or auto-number. 


If you want to create a custom field, you will have to populate the custom field object and explicitly call create() API for the custom field.    Something like this -


                CustomField field = new CustomField();
                field.setDescription("The Gaurav");
          AsyncResult[] ars = metadataConnection .create(new CustomField[] { field });


Balakrishna N 25Balakrishna N 25
Hi Gaurav

Thanks for sharing above code.
I want to create a custom filed on contact object so can you give some more elabration about the above code how we can achive this....


Thanks and Regards
N Balakrishna
Balakrishna N 25Balakrishna N 25

I found to create a custom filed in standard object
-->setup->Buid->customize->contact object->filed and Relationship--> New

softmain dotinsoftmain dotin

standard fields are creating same process like custom object
setup-Quick Search Box-type object name-select fields-filed and Relationship--> New

Thank You
Suraj Tripathi 47Suraj Tripathi 47
Hi gaurav.d,

You can create a custom field in a standard object like this:
Go to setup->Select Object Manager->Choose Object Name->Click field and relationships->New->choose type->Enter Name of field->Save.

If you find your Solution then mark this as the best answer.

Thank you!

Suraj Tripathi