Every business has different needs. Even if you sell the same products, you and your closest competitor still operate on a different business model. That’s why Salesforce API allows companies to create custom objects and custom fields.

There are two versions of the Salesforce applications – Lightning and Classic. On both of these versions, there are two ways of creating custom objects and fields. The traditional and easiest way is through the salesforce object manager. However, if you want to automate your salesforce process, it is recommended you create these customs using metadata API.

Traditional Method

Creating custom objects and fields in Salesforce API Lightening using the traditional method is simple. You start by logging into your salesforce developer account. On the homepage, choose to work in the “setup” option. In setup, you need to look for the “object manager” tab. This will open the object manager workspace for you. In that workspace, you want to click on the “create” button in the top right corner.

When creating an object, you will be asked to fill out information about the object, including the label name, label plural name, and a description. Once all that information is filled out, ensure that you save the new object.

Creating an object and field in Salesforce API Classic follows almost the exact same method. However, instead of searching for an “object manager” tab, you will simply search for the term “objects.” This will bring up your object manager, where you can create a new object using the same steps as above.

Metadata API

Creating a custom object and custom field with Metadata is much more complicated. It involves importing lines of code. If you are not familiar with the code, it is recommended that you stick with the traditional method.

To start creating custom objects and fields, you should open your salesforce workspace. Once you have agreed to the terms and conditions, you can log in with Salesforce. Once you are logged in, you will see a jump down menu that reads “action to perform.” Select Apex Execute in the jump menu. You will have to import a line of code that indicates whether you want to create a new object or a custom field.

Example Code for creating Custom Object
        MetadataService.MetadataPort metadataservice = new MetadataService.MetadataPort();
metadataservice .SessionHeader = new MetadataService.SessionHeader_element();
metadataservice .SessionHeader.sessionId = UserInfo.getSessionId();
List  fields = new List ();
MetadataService.CustomObject customobject = new MetadataService.CustomObject();
customobject.fullName = 'custom_create__c';
customobject.label = 'custom created object';
customobject.pluralLabel = 'custom created objects';
customObject.nameField = new MetadataService.CustomField();
customobject.nameField.type_x = 'Text';
customobject.nameField.label = 'status';
customobject.deploymentStatus = 'Deployed';
customObject.sharingModel = 'ReadWrite';
fields.add(customobject);
metadataservice.createMetadata(fields);

Once you have imported the code, you can go back to your Salesforce account, and search for “apex classes.”

apex_class_create_new

Click the “new” button, then paste the metadata from the following website: https://github.com/financialforcedev/apex-mdapi/blob/master/apex-mdapi/src/classes/MetadataService.cls. Once the metadata has been entered, you can run the apex code, and will be able to start creating custom objects and fields.

 

Example Code for creating Custom Field
        MetadataService.MetadataPort metadataservice = new MetadataService.MetadataPort();
metadataservice .SessionHeader = new MetadataService.SessionHeader_element();
metadataservice .SessionHeader.sessionId = UserInfo.getSessionId();
List fields = new List();
MetadataService.CustomField customField = new MetadataService.CustomField();
customField.fullName = ‘custom_create__c.custom_create_field__c’;
customField.label = ‘Custom created field’;
customField.defaultvalue = ‘false’;
customField.type_x = ‘Checkbox’;
fields.add(customField);
metadataservice.createMetadata(fields);

Again, this more complicated option is recommended for coding pros only. If you are not comfortable with code, the traditional method will still produce the results you want for your company. For more information, visit DCKAP.com.

Domnicamalan S

Domnicamalan S

Domnicamalan is a Software Engineer and Experienced in Python.

Leave a Reply