+ Start a Discussion
gaurav.dgaurav.d 

how to create custom field in standard object?

hi,

 

 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.setFullName(objectName+"__c");
                co.setDeploymentStatus(DeploymentStatus.Deployed);
                co.setDescription("Created by gaurav using the Metadata API");
                co.setLabel(displayName);
                co.setPluralLabel(displayName+"s");
                co.setSharingModel(SharingModel.ReadWrite);
                co.setEnableActivities(true);
                 
                // create the text id field
                String custom ="Test";
                CustomField field = new CustomField();
                field.setType(FieldType.Text);
                field.setDescription("The Gaurav");
                field.setLabel(displayName);
                field.setFullName(objectName+"__c");
                // add the field to the custom object
                co.setNameField(field);

 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");
                      return;
                  }
                   
                  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");
                          return;
                      }
                      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());
                      }
                      Thread.sleep(waitTimeMilliSecs);
                      // 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());
                }
    
    }

 

smitrasmitra

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?

sudhanshusudhanshu

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 

 

gmb_power_devgmb_power_dev

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.setType(FieldType.Text);
                field.setDescription("The Gaurav");
                field.setLabel(displayName);
                field.setFullName("Test4__c.CustomField__c");
          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....

please

Thanks and Regards
N Balakrishna
Balakrishna N 25Balakrishna N 25
Hi 

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


 
softmain dotinsoftmain dotin
Hello 

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

http://www.softmain.in/2017/08/how-to-create-custom-field-in.html

Thank You