Generally customers give us a excel sheet for the custom objects and the custom fields. It take too much time to create them manually.
So I have tried a code with the help of meta data api class , which can create all the custom objects and custom fields from CSV to SF in a single click.
There are two standard objects in SF which can be accessed via apex
1. CustomObject
2. CustomFields
So we need to do following steps.....
A. The sheet should be in a csv format because it returns a string.
B. Select the file which you want to create
C. Click Ok to create objects and their related fields.
So if you need the full code or demo then please let me know.
Thanks
Vikas Gaur
So I have tried a code with the help of meta data api class , which can create all the custom objects and custom fields from CSV to SF in a single click.
There are two standard objects in SF which can be accessed via apex
1. CustomObject
MetadataService.MetadataPort service = new MetadataService.MetadataPort(); service.SessionHeader = new MetadataService.SessionHeader_element(); service.SessionHeader.sessionId = UserInfo.getSessionId(); List<MetadataService.Metadata> fields = new List<MetadataService.Metadata>(); for(String s : objSet){ MetadataService.CustomObject customObject = new MetadataService.CustomObject(); customObject.fullName = s+'__c'; customObject.label = s; customObject.pluralLabel = s+'s'; customObject.nameField = new MetadataService.CustomField(); customObject.nameField.type_x = 'Text'; customObject.nameField.label = 'Name'; customObject.deploymentStatus = 'Deployed'; customObject.sharingModel = 'ReadWrite'; fields.add(customobject); } List<MetadataService.SaveResult> results = service.createMetadata(fields);
2. CustomFields
MetadataService.MetadataPort service = new MetadataService.MetadataPort(); service.SessionHeader = new MetadataService.SessionHeader_element(); service.SessionHeader.sessionId = UserInfo.getSessionId(); List<MetadataService.Metadata> fields1 = new List<MetadataService.Metadata>(); for(String s : multField){ MetadataService.CustomField customField = new MetadataService.CustomField(); customField.fullName = s.substringBefore('-')+'__c.'+s.substringBetween('&')+'__c'; customField.label = s.substringBetween('&'); if(s.substringAfter('#') == 'Text'){ customField.type_x = 'Text'; customField.length = 42; }else if(s.substringAfter('#') == 'Number'){ customField.type_x = 'Number'; customField.precision = 18; customField.scale = 2; }else if(s.substringAfter('#') == 'Checkbox'){ customField.type_x = 'Checkbox'; customField.defaultvalue = 'false'; }else if(s.substringAfter('#') == 'Email'){ customField.type_x = 'Email'; }else if(s.substringAfter('#') == 'Currency'){ customField.type_x = 'Currency'; customField.precision = 18; customField.scale = 2; } fields1.add(customField); } List<MetadataService.SaveResult> results = service.createMetadata(fields1);
So we need to do following steps.....
A. The sheet should be in a csv format because it returns a string.
B. Select the file which you want to create
C. Click Ok to create objects and their related fields.
So if you need the full code or demo then please let me know.
Thanks
Vikas Gaur