How to create Salesforce Object with Code(MetadataService Class)
#SalesforceOhana #Object #ApexCode #CreateSalesforceObjectThroughCode #Coding
What are Salesforce Objects?
=> Salesforce Objects are database tables in which we can store our data to a specific organization.
In Salesforce, we have two kinds of Objects.
- Standard Object:- The Objects which are already made by Salesforce itself. Forex. Account, Contact, Opportunity, etc. All are Salesforce Standard Objects.
- Custom Object:- The Objects which are made by the User itself.
Can you create Salesforce Custom Object through Code?
=> The answer is Yes, you can create Objects through Code. But, for this, you must have a MetaDataService Class in your org.
Here is a link, MetaDataSerivce Class.
Let's see how we can.
MetadataService.MetadataPort service = new MetadataService.MetadataPort();
service.SessionHeader = new MetadataService.SessionHeader_element();
service.SessionHeader.sessionId = UserInfo.getSessionId();
List<MetadataService.Metadata> metadata = new List<MetadataService.Metadata>();
MetadataService.CustomObject customobject = new MetadataService.CustomObject();
customobject.fullName = 'My_Custom_Object__c';
customobject.label = 'My_Custom_Object';
customobject.pluralLabel = 'My_Custom_Object';
customObject.nameField = new MetadataService.CustomField();
customobject.nameField.type_x = 'Text';
customobject.nameField.label = 'Custom created field';
customobject.deploymentStatus = 'Deployed';
customObject.sharingModel = 'ReadWrite';
metadata.add(customobject);
MetadataService.SaveResult[] results = service.createMetadata(metadata);
Thanks for reading and please drop your valuable feedback.
Thanks for sharing
ReplyDelete