4

Hi i have a small requirement ,i need to create a custom object in from one salesforce org to another salesforce org using rest API ,how can i achieve this using REST API .

2
  • Are you looking to copy the structure of a custom object, i.e. the metadata for it, or are you looking to copy an instance of the object? Commented Nov 15, 2013 at 11:24
  • @david Gillen am trying to create a object using metadata with help of REST API
    – Vineeth
    Commented Nov 15, 2013 at 11:30

2 Answers 2

4

Sadly the Metadata REST API was put on hold by Salesforce, so there is no REST API for Metadata (there is some limited support in the Tooling REST API, but not enough to create an object).

Currently you can only do this with the SOAP version of the API. While you can just send the raw XML requests over HTTP yourself, its recommended to use a compiler to generate the stubs to help with this.

There are a number of examples of consuming this from a Java client in the Metadata API Developers guide using the WSC (Web Service Connector) from Salesforce. If you want to do this from Apex there is also a library with examples here as well. Example from the WSC article as follows...

package wsc;

import com.sforce.soap.metadata.*;
import com.sforce.soap.partner.PartnerConnection;
import com.sforce.ws.ConnectionException;
import com.sforce.ws.ConnectorConfig;

public class Main {

  static final String USERNAME = "YOUR-USERNAME";
  static final String PASSWORD = "YOUR-PASSWORD&SECURITY-TOKEN";
  static PartnerConnection connection;

  public static void main(String[] args) throws ConnectionException {

    ConnectorConfig partnerConfig = new ConnectorConfig();
    ConnectorConfig metadataConfig = new ConnectorConfig();

    partnerConfig.setUsername(USERNAME);
    partnerConfig.setPassword(PASSWORD);

    @SuppressWarnings("unused")
    PartnerConnection partnerConnection = com.sforce.soap.partner.Connector.newConnection(partnerConfig);

    // shove the partner's session id into the metadata configuration then connect
    metadataConfig.setSessionId(partnerConnection.getSessionHeader().getSessionId());
    MetadataConnection metadataConnection = com.sforce.soap.metadata.Connector.newConnection(metadataConfig);

    // create a new custom object
    String objectName = "WSCCustomObject";
    String displayName = "WSC Custom Object";

    CustomObject co = new CustomObject();
    co.setFullName(objectName+"__c");
    co.setDeploymentStatus(DeploymentStatus.Deployed);
    co.setDescription("Created by the WSC using the Metadata API");
    co.setLabel(displayName);
    co.setPluralLabel(displayName+"s");
    co.setSharingModel(SharingModel.ReadWrite);
    co.setEnableActivities(true);

    // create the text id field
    CustomField field = new CustomField();
    field.setType(FieldType.Text);
    field.setDescription("The custom object identifier field");
    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());
    }      
  }
}

Using the Metadata File API. You could also consider using the Metadata API to retrieve the custom object in the XML format and deploy it in its XML form. The Metadata API has a file based API model for doing this, where by you receive a binary response that is in fact a zip file, which you can then pass on to another call (to your other org) to deploy it.

2
2

you can't with the Rest API, you need the Metadata API for that - http://www.salesforce.com/us/developer/docs/api_meta/api_meta.pdf

If you are an administrator and wants to deploy change from org A to org B, you can have a look at this app, which does exactly that: http://tquilatools.herokuapp.com/ (you need to create first a user, then you need to setup 2 connections on the home screen (one for the org A, another for the org B), and finally you have to go on the tab "Deploy Org 2 Org" and select the custom object you'd like to deploy.

If you're a dev and wants to code that, it will take you a long time to figure out, best is to start with the PDF I linked before.

2
  • Is this a commercial utility? Free tool? Can you make it clear, otherwise it looks a bit like self-promotion.
    – metadaddy
    Commented Nov 15, 2013 at 21:01
  • This is currently not commercial, this is free for "testing" and will be demonstrate at the Dreamforce. You may say it's self promotion, nevertheless it may help the OP - or anyone else. Commented Nov 17, 2013 at 14:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .