• sudhanshu
  • NEWBIE
  • 0 Points
  • Member since 2013

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies

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());
                }
    
    }