Tuesday, June 9, 2020

Salesforce Package Upload Automation

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 
  1. Description - Description of this version, what it contains
  2. Errors - will store errors, if upload failed 
  3. IsReleaseVersion - beta or released
  4. MajorVersion - like 2002 (i.e. 2020 2nd Package, in Salesforce terms Summer 20, Spring 20 etc.) 
  5. MetadataPackageId - Package id 
  6. MetadataPackageVersionId - Version Id, this id will be used in package url, to install it 
  7. MinorVersion - 2002.1 (First version for Summer 20)
  8. Password- Password if needed for installing this version
  9. PostInstallUrl - URL of the post-installation instructions
  10. ReleaseNotesUrl - URL of the package release notes
  11. Status -  Success or Failure, after upload
  12. VersionName - Name of version (Summer 20, Spring 20 etc)
CreatedDate (Field Exists but not mentioned in the document, Raised the doc bug for it)


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.