function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
mani toolmani tool 

Method does not exist or incorrect signature: void createService() from the type anon

I am trying to create objects and fields dynamically using apex code, But I'm getting the error "Method does not exist or incorrect signature: void createService() from the type anon". I have deployed the metadataservice class and related classes in my Org. I try to create an object using below code:



 MetadataService.MetadataPort service = createService();
        MetadataService.CustomObject customObject = new MetadataService.CustomObject();
        customObject.fullName = 'Test__c';
        customObject.label = 'Test';
        customObject.pluralLabel = 'Tests';
        customObject.nameField = new MetadataService.CustomField();
        customObject.nameField.type_x = 'Text';
        customObject.nameField.label = 'Test Record';
        customObject.deploymentStatus = 'Deployed';
        customObject.sharingModel = 'ReadWrite';
        List<MetadataService.SaveResult> results =
            service.createMetadata(
                new MetadataService.Metadata[] { customObject });
        handleSaveResults(results[0]);

I have executed the above code and I got the error:  I am getting the error because I don't have Createservice() method in my metadataservice class. Can anyone help me how to fix this?
Best Answer chosen by mani tool
RatanRatan
it is not
service.create(fields)

it is createMetadata
 
service.createMetadata(fields);


check this class and it's methods 


User-added image

https://raw.githubusercontent.com/financialforcedev/apex-mdapi/master/apex-mdapi/src/classes/MetadataService.cls
 

All Answers

RatanRatan
Instead of 
MetadataService.MetadataPort service = createService();

use 
MetadataService.MetadataPort service = new MetadataService.MetadataPort();

This way you can create new service check this post for more info https://salesforce.stackexchange.com/a/112138/18731 

 
mani toolmani tool
Thanks Ratan,

Now iam getting another error : "Method does not exist or incorrect signature: void create(List&lt;MetadataService.Metadata&gt;) from the type MetadataService.MetadataPort"
RatanRatan
Can you share your code here? 
mani toolmani tool
MetadataService.MetadataPort service = new MetadataService.MetadataPort();  
service.SessionHeader = new MetadataService.SessionHeader_element();
service.SessionHeader.sessionId = UserInfo.getSessionId();
//system.debug('session id:'+UserInfo.getSessionId());
System.debug(UserInfo.getOrganizationId()+''+UserInfo.getSessionId().SubString(15));

List<MetadataService.Metadata> fields = new List<MetadataService.Metadata>();
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 = 'Custom created field';
customobject.deploymentStatus = 'Deployed';
customObject.sharingModel = 'ReadWrite';
fields.add(customobject);

MetadataService.AsyncResult[] results = service.create(fields);
RatanRatan
it is not
service.create(fields)

it is createMetadata
 
service.createMetadata(fields);


check this class and it's methods 


User-added image

https://raw.githubusercontent.com/financialforcedev/apex-mdapi/master/apex-mdapi/src/classes/MetadataService.cls
 
This was selected as the best answer
mani toolmani tool
Thanks Ratan,

Got another error : "System.CalloutException: IO Exception: Unauthorized endpoint, please check Setup-&gt;Security-&gt;Remote site settings. endpoint = https://ap5.salesforce.com/services/Soap/m/38.0" . Do i need to create any remote site settings for this.
mani toolmani tool
Your Response is much Appreciable @Ratan.
RatanRatan
yes, you need to create a remote site with https://ap5.salesforce.com URL. 
mani toolmani tool
Thank you verymuch