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
Saptarshi SahaSaptarshi Saha 

Invalid type: ReadResult error showing even after creating Apex class from Metadata WSDL file

Below is code of the Apex class - 
public class Test2 {
    
    public static void GetEntitlementProcess(){
        
       ReadResult readResult = metadataConnection.readMetadata('EntitlementProcess','PRAS 01');     
        
    }

}

Whenever I save this class I am getting the error "Invalid type: ReadResult". I generated the Metadata WSDL and then generated the Apex class from the "Generate from WSDL" button in Apex Class section in salesforce. But still when I make the metadata call I receive the above error. I even tried referring the ReadResult datatype by using the Apex class generated from WSDL with the dot operator before the name (ClassName.Readresult). But still the error persists. Any solutions ?
ForceMantis (Amit Jain)ForceMantis (Amit Jain)
It will be worth having a look at Apex Wrapper for Metadata API (https://github.com/financialforcedev/apex-mdapi) instead of importing metadata wsdl and generating apex class yourself. Apex Wrapper for Metadata API is an oper source contribution by Force.com MVP Andrew Fawcett and we have used this in many impelementations.
Saptarshi SahaSaptarshi Saha
@Jain thank you. But I need use the generated Apex class that I created from Metadata API WSDL. I have seen the wrapper as suggested by you and others. But I need to do it my self. Can you suggest me what I should do with my generated Apex class or if I am doing something wrong with it. Also since I am a newbee to salesforce and IT, is it okay to use the open source Apex Wrapper for commercial projects.
ForceMantis (Amit Jain)ForceMantis (Amit Jain)
If you are newbee to salesforce and IT it will be certainly difficult to understand and work using generated class using wsdl. Salesforce wsdl to apex parser is not very good. Apex Wrapper that I referred is also generated using WSDL to Apex Generator along with some hacks and additional code make our life easy.

It is certainly ok to use this wrapper for commercial project but you will need to retain copyright header of author, check with legal team of your company if they have a issue with it, but I don't see a problem in it.

Still if you really want to do it you have to kind of reinvent the wheel and apply hacks that Andrew Fawcett applied while writing this wrapper.
Saptarshi SahaSaptarshi Saha
@Jain what are these hacks? 
VineetKumarVineetKumar
Just check the return type of metadataConnection.readMetadata method.
Also, if possible can you point me to the WSDL, maybe I can generate the classes in my devorg and have a look.
Technically, it should have been 
metadataConnection.ReadResult readResult = metadataConnection.readMetadata('EntitlementProcess','PRAS 01');   
But since you have already tried this out, can't be sure of the other issue.
Saptarshi SahaSaptarshi Saha
You can get the Metadata API from the Setup > Quick Response > API (under the Develop option). There will be a link to download the Metadata API
VineetKumarVineetKumar
Well from the WSDL, I can see that the return type of readMetadata function is readMetadataResponse.
So, it should be something like :
metadataConnection.readMetadataResponse readResult = metadataConnection.readMetadata('EntitlementProcess','PRAS 01');
Saptarshi SahaSaptarshi Saha
Are you looking in raw XML file that you get when clicking on the link?
VineetKumarVineetKumar
No after consuming the WSDL.
Saptarshi SahaSaptarshi Saha
Are you getting two classes like these ones AsyncSoapSforceCom200604Metadata, 
soapSforceCom200604Metadata when you generate the Apex class from the downloaded Metadata file ?