Uploading a package is big and tedious process if you have several components, even opening package upload page may take more than 10-15 minutes.
What if requirement is to build the org and get package daily. Which can take more than 4 hours to get the package out.
I have a solution to above problem 😃
PackageUploadRequest is the key for us to get success on this.
When user clicks on upload button on package creation page, PackageUploadRequest record gets created in background.
Salesforce has given access to this object using Tooling API, we can insert record to it, which will upload the package.
Fields
- Description - Description of this version, what it contains
- Errors - will store errors, if upload failed
- IsReleaseVersion - beta or released
- MajorVersion - like 2002 (i.e. 2020 2nd Package, in Salesforce terms Summer 20, Spring 20 etc.)
- MetadataPackageId - Package id
- MetadataPackageVersionId - Version Id, this id will be used in package url, to install it
- MinorVersion - 2002.1 (First version for Summer 20)
- Password- Password if needed for installing this version
- PostInstallUrl - URL of the post-installation instructions
- ReleaseNotesUrl - URL of the package release notes
- Status - Success or Failure, after upload
- VersionName - Name of version (Summer 20, Spring 20 etc)
Different Scenarios :-
Upload new package with version name and number
packageUploadRequestObj.setMetadataPackageId(packageId);
packageUploadRequestObj.setVersionName(packageVersionDescription);
packageUploadRequestObj.setDescription(packageDescription);
packageUploadRequestObj.setMajorVersion(packageMajorVersionNumber);
packageUploadRequestObj.setMinorVersion(packageMinorVersionNumber);
packageUploadRequestObj.setPostInstallUrl(packagePostInstallUrl);
packageUploadRequestObj.setReleaseNotesUrl(packageReleaseNotesUrl);
packageUploadRequestObj.setIsReleaseVersion(isReleaseVersion);
packageUploadRequestObj.setPassword(packagePassword);
Upload Subsequent number package i.e. 2002.2 when 1 was previously uploaded.
Query Previous Successful Upload (MetadataPackageVersion)
While uploading release package -
connection.query("SELECT Id,Name,MajorVersion,PatchVersion,MinorVersion FROM MetaDataPackageVersion where ReleaseState = 'Released' AND PatchVersion=0 order by MajorVersion desc,MinorVersion desc limit 1 ");
While uploading beta package -
connection.query("SELECT Id,Name,MajorVersion,PatchVersion,MinorVersion FROM MetaDataPackageVersion order by MajorVersion desc,MinorVersion desc limit 1 ");
Add 1 to minor version and use the same while inserting record in PackageUploadRequest.
So minor version would be 2 and so on.
This way we don't need to mention number and name everyday while uploading, it will automatically take the name and number from previous record and insert the record
Upload Patch Package
Here we just copy the same query with a minor modification PatchVersion>0
connection.query("SELECT Id,Name,MajorVersion,PatchVersion,MinorVersion FROM MetaDataPackageVersion where ReleaseState = 'Released' AND PatchVersion>0 order by MajorVersion desc,MinorVersion desc limit 1 ");
Copy all fields as it is and insert the record in patch org.
Hope this helps, please fill free to comment in case of questions.