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
Pablo RoldanPablo Roldan 

Metadata ApexClass type from apex class

Hi All,

I get this error all the time when I try to create an 'ApexClass' from one apex class using metadata:
Web service callout failed: WebService returned a SOAP Fault: INVALID_TYPE: This type of object is not available for this organization faultcode=sf:INVALID_TYPE faultactor=

The code is similar than create an 'ApexPage', although 'ApexPage' works.
So could you help me with this?

Code to create 'ApexClass':
public PageReference mCreateApexClass(){
  MetadataService.MetadataPort service = createService();    
     MetadataService.ApexClass lvApexClass = new MetadataService.ApexClass();
     lvApexClass.apiVersion = 29;
     lvApexClass.fullName = myApexClassName;
     lvApexClass.status = 'Active';
     
     lvApexClass.content = EncodingUtil.base64Encode(Blob.valueOf(myApexClassContentAsString));
     MetadataService.AsyncResult[] results = service.create(new List<MetadataService.Metadata> { lvApexClass });
     result = results[0];
     displayStatus();
     return null;
}

 

Thanks in advance.
Best Answer chosen by Pablo Roldan
Pablo RoldanPablo Roldan
Hi All,

After a lot of test, I have resolved it. It was a problem when the zip to upload was built.

Thanks everyone for the help...

All Answers

Pablo RoldanPablo Roldan
Hi All,

I have been looking for a solution, I have been able to see that nobody talks about that. There are articles about 'ApexPages' 'CustomObject', 'CustomFIeld'...but nothing about 'ApexClass'.
So I would have to guess that it's not possible to create apex classes through the metadata or it's that nobody has tried before me?

Thanks in advance.
Pablo RoldanPablo Roldan
Hi All,

I have investigated about that, and I have been able to see that it's not possible to do the create method with ApexClass and ApexTrigger through metadata.
So, I have seen some confusing articles, which suggest that it is possible to create ApexClass, through the deploy method in metadata.
After a lot of tries, I have done a method which can do the deploy, but I can't see that the ApexClass was created.

Has anyone any idea about that or anything about metadata???

This is the code:

public PageReference mCreateApexClass(){
MetadataService.MetadataPort service = createService();
    
MetadataService.DeployOptions lvDeployOption = new MetadataService.DeployOptions();
lvDeployOption.allowMissingFiles = false;
lvDeployOption.autoUpdatePackage = true;
lvDeployOption.checkOnly = false;
lvDeployOption.ignoreWarnings = true;
lvDeployOption.performRetrieve = false;
lvDeployOption.purgeOnDelete = false;
lvDeployOption.rollbackOnError = true;
lvDeployOption.runAllTests = false;
lvDeployOption.runTests = new String[]{};
lvDeployOption.singlePackage = true;
       
result = service.deploy(EncodingUtil.base64Encode(Blob.valueOf('public class test{}')),lvDeployOption);
displayStatusDeploy();
return null
}

Thanks in advance
Pablo RoldanPablo Roldan
Hi All,

After a lot of test, I have resolved it. It was a problem when the zip to upload was built.

Thanks everyone for the help...
This was selected as the best answer
PriyasoftPriyasoft
Hi Roldan,

Sounds good that you resolved this issue.
I am facing same exception but I dint get you what do you mean by 'It was a problem when the zip to upload was built'.
Could you please throw some more light on the solution.

Thanks
Pablo RoldanPablo Roldan
Hi Priyasoft,

If you have done any retrieve about the apex code, you will understand that better and quickly.
When you retrieve the apex code from salesforce, you need to use "unzip" compononent which unpacked your apex code in three different strings (package, metadata and code).
So when you want to create a new apex class, you need another compenent which is zip, this packed your three different strings into a apex class creating that in salesforce.

I hope that this help you, if not please ask me again.
PriyasoftPriyasoft
Hi Roldan,

Thanks for your reply.
Below is my sample code with which I am facing probem.
I appreciate your help if you tell me what's wrong with the below code or else please share me your working code.

    MetadataService.MetadataPort service = createService();    
    MetadataService.ApexClass apexClass = new MetadataService.ApexClass();
    apexClass.apiVersion = 26;
    apexClass.status='Active';   
    String classContent = 'public class SampleClass1{}';
    apexClass.content = EncodingUtil.base64Encode(Blob.valueOf(classContent));    
    MetadataService.AsyncResult[] results = service.create(new List<MetadataService.Metadata> { apexClass });

Thanks in advance




neao18neao18
Hi Roldan,

I am also tring the same thing but stuck at one place,
I have managed to get the class,meta and xml file from the zip by using JSZip.js lib.
But when I make some changes to class file and again rezip it by using JSZip.js lib, it agin gives me the same error. Any advice!!
Shikha Raheja 3Shikha Raheja 3
Hi Roldan,

Below is my code that I am trying to update api version of a class.

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

MetadataService.ApexClass apexClass = new MetadataService.ApexClass();
apexClass.fullName = 'AccountHandler';
apexClass.apiVersion = 41.0;
apexClass.Status='Active';
MetadataService.saveResult[] results = 
    service.updateMetadata(new List<MetadataService.Metadata> {apexClass});

However,I am getting below error:
System.CalloutException: Web service callout failed: WebService returned a SOAP Fault: INVALID_TYPE: This type of object is not available for this organization faultcode=sf:INVALID_TYPE faultactor=

Not sure what os wrong here. Could you please suggest?
Alex Edelstein 9Alex Edelstein 9
I ran into this problem and figured it out. Here's more detail.

ApexClasses have extra limitations that prevent them being used with MetadataService.updateMetadata or MetadataService.create. The only way to deploy them is to use MetadataService.deploy. This involves literally assemplying a zipped package, complete with a package.xml.

There's actually a pretty good description of this here, in the section 'Metadata Retrieve Demo'. (https://github.com/financialforcedev/apex-mdapi/blob/master/README.markdown)

Here's working code that does a deploy:
https://gist.github.com/alexed1/7f64604d317278b10a7d03493aeb31a8