System.requestVersion() does not work in Second Generation Packaging

Environment

  • sfdx-cli/7.58.2 win32-x64 node-v12.16.3
  • API 48

What’s going on?

When trying to create a managed packaged version with Second Generation Packaging (2GP) -with code check- that contains this class:

global with sharing class VersionClass {
    global static void controlMethod() {
        String foo = 'someString';
    }

    global static void versionMethod() {
        Version packageVersion = System.requestVersion();
    }
}

the following error occur when calling the method containing System.requestVersion(); 1

(1) Apex Test Failure: Class.foobarbaz.VersionClass.versionMethod: line 8, column 1
  Class.foobarbaz.TestVersionClass.test_versionMethod: line 17, column 1 System.ProcedureException: Method is not supported from an unmanaged namespace

Such errors do not occur with the First Generation Packaging.

What’s the expected behavior?

We expect the managed package version to be created without any errors while using 2GP.

How to reproduce?

  1. Clone the MRC Repo
  2. Ensure that you’re setup for 2GP
  3. Try to create a version. You should get the aforementioned error.

Notes

1: This occurs outside of Apex test calls as well. We originally found the issue by calling a similar method through a RemoteAction in a VisualForce Page. But let’s keep it simple for the example sake.

Answer

After contacting Salesforce, I had confirmation that Version is, to this day, not supported in 2GP, which caused the issue described here.

Update Oct.2021: still not supported.

The “least worst” workaround I found so far would be to generate an ApexClass w/ a static field or CustomMetadata that would contain the version, in a CI/CD.

Attribution
Source : Link , Question Author : Louis Bompart , Answer Author : Louis Bompart

Leave a Comment