0
MetadataService.MetadataPort service = new MetadataService.MetadataPort();
service.SessionHeader = new MetadataService.SessionHeader_element();
service.SessionHeader.sessionId = UserInfo.getSessionId();

MetadataService.CustomField customField = new MetadataService.CustomField();
customField.fullName = 'Test__c.TestField__c';
customField.label = 'Test Field';
customField.type_x = 'Checkbox';
 MetadataService.AsyncResult[] results =
service.create(new List<MetadataService.Metadata> { customField });

I am trying to create fields with the above code but I am getting Invalid type: MetadataService.MetadataPort Error. Can we create different types of fields using metadata? if so what is the issue with the code?

0

You can create Checkbox field using MetadataService however in your case it seems like you don't have the MetadataService class in your org where you are running the code. Code sample you are trying to use is part of apex-mdapi wrapper.

Below code works fine after having the necessary library in the org. This code creates a checkbox field on Account object.

MetadataService.MetadataPort service = new MetadataService.MetadataPort();
service.SessionHeader = new MetadataService.SessionHeader_element();
service.SessionHeader.sessionId = UserInfo.getSessionId();

MetadataService.CustomField customField = new MetadataService.CustomField();
customField.fullName = 'Account.TestField__c';
customField.label = 'Test Field';
customField.type_x = 'Checkbox';
customField.defaultValue = 'false';
List<MetadataService.SaveResult> results = service.createMetadata(new MetadataService.Metadata[] { customField });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.