12.07.2015 Views

Database.com Metadata API Developer's Guide - Salesforce.com

Database.com Metadata API Developer's Guide - Salesforce.com

Database.com Metadata API Developer's Guide - Salesforce.com

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CRUD-Based <strong>Metadata</strong> DevelopmentNote the error handling code that follows each <strong>API</strong> call.import <strong>com</strong>.sforce.soap.metadata.*;/*** Sample that logs in and creates a custom object through the metadata api*/public class CRUDSample {private <strong>Metadata</strong>Connection metadataConnection;// one second in millisecondsprivate static final long ONE_SECOND = 1000;public CRUDSample() {}public static void main(String[] args) throws Exception {CRUDSample crudSample = new CRUDSample();crudSample.runCreate();}/*** Create a custom object. This method demonstrates usage of the* create() and checkStatus() calls.** @param uniqueName Custom object name should be unique.*/private void createCustomObject(final String uniqueName) throws Exception {final String label = "My Custom Object";CustomObject customObject = new CustomObject();customObject.setFullName(uniqueName);customObject.setDeploymentStatus(DeploymentStatus.Deployed);customObject.setDescription("Created by the <strong>Metadata</strong> <strong>API</strong> Sample");customObject.setLabel(label);customObject.setPluralLabel(label + "s");customObject.setSharingModel(SharingModel.ReadWrite);etc");// The name field appears in page layouts, related lists, and elsewhere.CustomField nf = new CustomField();nf.setType(FieldType.Text);nf.setDescription("The custom object identifier on page layouts, related listsnf.setLabel(label);nf.setFullName(uniqueName);customObject.setNameField(nf);AsyncResult[] asyncResults = metadataConnection.create(new CustomObject[]{customObject});if (asyncResults == null) {System.out.println("The object was not created successfully");return;}long waitTimeMilliSecs = ONE_SECOND;// After the create() call <strong>com</strong>pletes, we must poll the results of the checkStatus()// call until it indicates that the create operation has <strong>com</strong>pleted.do {printAsyncResultStatus(asyncResults);waitTimeMilliSecs *= 2;Thread.sleep(waitTimeMilliSecs);asyncResults = metadataConnection.checkStatus(newString[]{asyncResults[0].getId()});} while (!asyncResults[0].isDone());}printAsyncResultStatus(asyncResults);22

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!