1

Is there a way to validate Salesforce package id? I don't know if there is a database where you can query to see if the package ID is the valid or at least can I validate to see if the format of the package id is valid ?

04t3k000001liYr -> here is the valid package id

123123213sadf12311 -> invalid package id.

  • That's a Custom Object Id, not a Package Id. What exactly are you trying to do? – sfdcfox Nov 16 at 0:58
  • That’s correct, I was trying to give an example, I’m trying to validate package Id if the id is valid or invalid. – Nick Kahn Nov 16 at 1:33
  • updated the question with correct package id. – Nick Kahn Nov 16 at 2:47
1

As per the documentation, Package ID values are in the form of 033xx00000xxxxxYYY, and Package Version ID values (what you typically use to install a package) are in the form of 04txx00000xxxxxYYY, where x is a base-62 number, and YYY is the case-sensitivity parity bits. You can read more about What are Salesforce ID's composed of for calculating the parity bits.

Basically, to "verify" that the ID is at least potentially valid, the first three characters should be 04t, the total length should be 15 or 18 characters, all characters should be in [0-9A-Za-z], and the last 3 characters should calculate to a valid parity bit, if the length is 18 characters.

Here's some sample Apex code that should work:

public static Boolean isValidPackageVersionId(String packageId) {
    try {
        return Id.valueOf(packageId).getSObjectType() + '' == 'AllPackageVersion';
    } catch(Exception e) {
        return false;
    }
}

For some reason, AllPackageVersion isn't a valid sObjectType class, so we convert it to a String and check from there. This should work in any version of Apex, for both 15- and 18-character ID types (invalid type conversions and nulls are also handled by this code).

You can't precisely "query the database" for these values, at least not in Apex, but you can use sfdx force:package:version:list to get a list of valid package IDs in your org.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.