A Python Tool to Upgrade Apex Wrapper Salesforce Metadata API Version
Apex Wrapper Salesforce Metadata API provides a powerful solution for generating Salesforce metadata dynamically within Apex. However, the version contained in this package is currently 42.0. To leverage the latest metadata version, it is necessary to upgrade the Apex wrapper. I followed the detailed instructions for doing at this link. but when generating apex from metadata file, I got the below due to the resulting classes exceed the 1 million character limit on Apex classes.
After conducting some research, it appears that the primary workaround for the issue of large metadata file sizes is to either split the metadata into multiple smaller files or selectively extract only the necessary metadata types into a separate file. To facilitate this process, I created a Python tool that allows me to extract necessary metadata types and patch the resulting class with the latest API version on my local machine. Then, I can save it in a Salesforce org. You can find and utilize the tool from this GitHub repository.
Install the Tool: Follow the instructions in the README file to install the tool.
Download
metadata.wsdl
File: Download this file from your Salesforce org.Extract Specific Metadata: Run the command to extract specific metadata such as Flow and CustomObject (You can add more metadata types using comma separated value).
sfmetadataextractor extract -i metadata.wsdl -o metadata_extracted.wsdl -m Flow,CustomObject
Generate Mapping JSON File: Create a mapping JSON file for patching the Apex class, this file will be used later in Step 6
Reason to patch: The WSDL file may use an inheritance model that is not directly supported by the generated Apex code. This can lead to issues where the generated code does not correctly reflect the structure and relationships defined in the WSDL. So we need to patch the class using the extension_mapping.json file
sfmetadataextractor extensionMap -i metadata.wsdl -o extension_mapping.json
Generate Apex Class: Use
metadata_extracted.wsdl
to generate an Apex class namedMetadataServiceImported.cls
in your Salesforce org.Copy Apex Class to Local File: Copy the content of
MetadataServiceImported.cls
to a local file.Patch Apex Class: Patch the Apex class and rename it to
MetadataService.cls
(version 61.0 in the example below, change it to the version you prefer)sfmetadataextractor patch -e extension_mapping.json -i MetadataServiceImported.cls -o MetadataService.cls -a 61.0
Generate Unit Tests: Generate unit tests named MetadataServiceTest.cls for the Apex class
MetadataService.cls for deployment
.sfmetadataextractor generateUnitTests -i MetadataService.cls -o MetadataServiceTest.cls
Now you have
MetadataService.cls and MetadataServiceTest.cls files that can be saved back to your Salesforce org
Hope this would be useful to solve the issue with upgrading the API version for the apex wrapper. If you have any questions or suggestions, please feel free to contact me.