+ Start a Discussion
Uma Shankar.ax1909Uma Shankar.ax1909 

want to use createfield method through this code but it's give error invalid type

I use this code:::
public static void createField(){
    MetadataService.MetadataPort service = createService();     
    MetadataService.CustomField customField = new MetadataService.CustomField();
    customField.fullName = 'Test__c.TestField__c';
    customField.label = 'Test Field';
    customField.type_x = 'Text';
    customField.length = 42;
    MetadataService.AsyncResult[] results = service.create(
       new List<MetadataService.Metadata> { customField });


I want to create this metadataservice.metadataport instance but i can't use. It's gives ERROR invalid types. How can i use this, plz help me, Your response is highly appriciated.




Uma shankar



















The metadata APis are not to be used in Apex for creating fields on the fly. You can use these APis from outside or making a callout from Apex to other org.




To create Text field for using MetadataAPI


 MetadataService.MetadataPort service = MetadataService.createService(); 
    MetadataService.CustomField customField = new MetadataService.CustomField();

customField.fullName ='Test__c.'+TestField__c;
    customField.label = Test Field;    
    customField.type_x = 'Text';
    customField.length = 25;
    MetadataService.AsyncResult[] results1 = service.create(new List<MetadataService.Metadata> { customField });


try this code and will create text field.