7

How to update API version of all classes in single stretch

2
  • 1
    hi user23820, have you already considered some options or done some research ? You'll probably have to create some script to do this on your side, and commit everything back to a sandbox using the metadata api Aug 10, 2015 at 8:09
  • 1
    Why would you want to update all classes API version?
    – Doug B
    Aug 10, 2015 at 8:43

2 Answers 2

6

You could pull all the apex classes down via the Metadata API retrieve(). Then use a text editor to find/replace the required API version in the .meta files. Then, again using the metadata API, deploy them all back again using deploy().

Of course, it might not be that easy. There might be individual incompatibilities between the API versions that you will need to address.

You could probably substitute another tool for the Metadata API, such as the Eclipse IDE or MavensMate. Anything that will put the files on disk where you can easily modify them in bulk with a tool of your choosing.

5

Salesforce CLI is the best option when it comes to automating such processes. So you can create and run a script.

For Scratch Orgs we are assuming that your local git is the source of truth of your project and every Apex Class is already synced. Assume that the script file and your target directory (force-app by default) are at the root of the project:

root
-> force-app/
-> update_api_version_of_apex_classes.sh

update_api_version_of_apex_classes.sh

# Set vars
elem="apiVersion"
value=54.0

# Update "apiVersion" value for each Apex Class in "force-app" directory and subdirectories

# Find all files with names end with ".cls-meta.xml"
# and set new "apiVersion" value for each file found
find force-app -type f -name "*.cls-meta.xml" \
    -exec sed -i "s|<$elem>[0-9]*[0-9.]*[0]*</$elem>|<$elem>$value</$elem>|" '{}' \;

# Push changes to the default scratch org
sfdx force:source:push

For Sandboxes it makes sense (depending on the specific case and goals) to pre-pull all Apex Classes. Also, it is possible now to use Salesforce CLI to retrieve and deploy metadata to non-source-tracked orgs with the same ease of pushing and pulling source to and from scratch orgs without extra conversion steps. The only thing to do is to establish connection to the sandbox.

root
-> src/classes/
-> update_api_version_of_apex_classes.sh

update_api_version_of_apex_classes.sh

# Set vars
elem="apiVersion"
value=54.0

# Retrieve all Apex Classes 
sfdx force:source:retrieve -m ApexClass -u [email protected]

# Update "apiVersion" value for each retrieved Apex Class in "src/classes" directory

# Find all files with names end with ".cls-meta.xml"
# and set new "apiVersion" value for each file found"
find src/classes -type f -name "*.cls-meta.xml" \
    -exec sed -i "s|<$elem>[0-9]*[0-9.]*[0]*</$elem>|<$elem>$value</$elem>|" '{}' \;

# Deploy all the Apex Classes back to the sandbox
sfdx force:source:deploy -m ApexClass -u [email protected]

Docs:

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .