System.TypeException: meaning and how to fix it

Apex runtime Checked against the official docs on Source: SOAP API Developer Guide: Core Data Types, StatusCode

Quick answer

System.TypeException: A cast or conversion is not valid for the actual runtime type of the value, or a string could not be converted to a number, date or ID. Start with: Check the real type with instanceof before casting.

Typical message: System.TypeException: Invalid conversion from runtime type Account to Contact

Cause

A cast or conversion is not valid for the actual runtime type of the value, or a string could not be converted to a number, date or ID.

How to fix System.TypeException

  1. Check the real type with instanceof before casting.
  2. Validate strings before converting them to numbers or dates.

Have a different message? Paste it into the error decoder, which matches it against 45 common Salesforce errors in your browser.

Frequently asked questions

What does System.TypeException mean in Salesforce?

A cast or conversion is not valid for the actual runtime type of the value, or a string could not be converted to a number, date or ID.

How do I fix System.TypeException?

1. Check the real type with instanceof before casting. 2. Validate strings before converting them to numbers or dates.

More Apex runtime errors

SFDX Talk is unofficial documentation. It is not affiliated with, endorsed by, or sponsored by Salesforce, Inc. Message text varies slightly by release and context, so confirm against the official page before relying on it.