function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
jhartjhart 

Bug (minor): System.Version class isn't serializable = failure to executeBatch()

Writing some InstallHandler code for upgrade scripts.

 

Using Database.executeBatch, naturally, for large-scale data changes.

 

One of our classes that implements Database.Batchable has an instance field of type System.Version (to track the version that this particular batch job is doing the upgrade work for).

 

Turns out the System.Version class - which is just 3 integers - isn't serializable ... and as a result the call to executeBatch fails:

 

System.SerializationException: Not Serializable: system.Version

 

Now, System.Version is just three integers, so I expect this lack of serializability was a simple oversight in the underlying code.

 

 

For the moment we're working around it with our own "Version" class:

 

// We can't have "Version" as an instance field b/c it's not serializable (wut)
// So we have to roll our own here
public class V {
  public final integer major, minor, patch;
  public V(Version v) {
    this.major = v.major();
    this.minor = v.minor();
    this.patch = v.patch();
    }
  public Version version() {
    return new Version(major, minor, patch);
    }
  }

 

That *is* serializable, so can be happily reference by a Database.Batchable implementation.

jhartjhart

To further the weirdness, you actually have to choose your Version constructor carefully depending on the nullity of the patch number:

 

public class V {
  public final integer major, minor, patch;
  public V(Version v) {
    this.major = v.major();
    this.minor = v.minor();
    this.patch = v.patch();
    }
  public Version version() {
    return patch == null
      ? new Version(major, minor)
      : new Version(major, minor, patch);
    }
  }

Because if you pass "null" as the patch number, like so:

 

Version v = new Version(1,1,null);

You get an UNKNOWN_EXCEPTION.

 

UNKNOWN_EXCEPTION: An unexpected error occurred. Please include this ErrorId if you contact support: 417190567-10795 (-405031490) at 

 

Not exactly bulletproof, ye olde Version class....