---
schema_version: "1"
id: 8daebaa8c650a92d3d425c3aecb82248fbc4cd3ea486a9da3993471e9ab5862b
title: Versioning Apex Code
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/platform-pkg-exchange/managed_packaging/guides/first-generation/packaging-behavior-apex.md
canonical_url: https://developer.salesforce.com/docs/platform/pkg1-dev/guide/packaging-behavior-apex.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: eddf7cb225d48aa5964cb0f8774bab7aa8cf436f4f24c2400a6ff626db3237fa
products:
  - First-Generation Managed Packaging Developer Guide
  - merged-docs
---

# Versioning Apex Code

When subscribers install multiple versions of your package and write code that references Apex classes or triggers in your package, they must specify the version that they’re referencing.

Within the Apex code that is being referenced in your package, you can conditionally execute different code paths based on the version setting of the calling Apex code that is making the reference. The package version setting of the calling code can be determined within the package code by calling the `System.requestVersion` method. In this way, package developers can determine the request context and specify different behavior for different versions of the package.

The following sample shows different behavior in a trigger for different package versions:

<!-- otherprops: set_clipboard -->

```apex_code
trigger oppValidation on Opportunity (before insert, before update) {

    for (Opportunity opp : Trigger.new){

        // Add a new validation to the package
        // Applies to versions of the managed package greater than 1.0
        if (System.requestVersion().compareTo(new Version(1,0)) > 0) {
            if (opp.Probability >= 50 && opp.Description == null) {
                opp.addError('All deals over 50% require a description');
            }
        }

        // Validation applies to all versions of the managed package.
        if (opp.IsWon == true && opp.LeadSource == null) {
            opp.addError('A lead source must be provided for all Closed Won deals');
        }
    }
}
```

To compare different versions of your Apex classes, click the **Class Definition** tab when viewing the class details.

For more information about the `System.requestVersion` method, see the [Apex Developer Guide](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_version.htm).
