Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm working on integrating Salesforce with an external system and am having some trouble with my approach. One of the REST endpoints I've made in Salesforce is expecting a 'person' object that could be either a lead or an account (we're using person accounts, but I don't think that's relevant). The endpoint takes the JSON and deserializes it into a virtual class called Person. I think check a flag on the object to see if it's a lead or an account - from there, I then want to cast the object as either a lead or account object (which I'll then do some more checks, updates, etc before ultimately converting it into an actual Salesforce lead or account.

Here's a simplified version of my classes:

global virtual class CustomPerson {
 public String FirstName {get;set
 public Boolean IsLead {get;set;}
 public String LastName {get;set;}
 public JasmineLead toLead() {
  return (CustomLead)this;

global class CustomLead extends CustomPerson {
 public CustomLead () {
  //Do some more stuff here

In my REST endpoint, it's doing this:

//Cast the JSON to the CustomPerson class - works fine
CustomPerson customPerson = (CustomPerson)JSON.deserialize(jsonString, CustomPerson.class);
if(customPerson.IsLead) {
 //This next line throws an error when I post to the endpoint
 CustomLead customLead = customPerson.toLead(); 
} else {

Everything saves/compiles correctly, but when I post to the endpoint & it gets to this this:

CustomLead customLead = CustomPerson.toLead();

I get this error:

System.TypeException: Invalid conversion from runtime type CustomPerson to CustomLead

Anything obvious that I'm doing wrong? Is there a better approach I should take?

share|improve this question
up vote 4 down vote accepted

Your customPerson variable isn't a CustomLead even though you feel like it should be. In order for casting to work, the variable you're casting must truly be that type (or one of its descendent types). When you deserialized the JSON, you told the deserializer to instantiate a new CustomPerson instance and it can never be cast to a subclass type.

Instead, your toLead() method is going to need work .. something like (pseudocode)

public CustomLead toLead()
  CustomLead customLead = new CustomLead();
  //Copy all the fields over
  customLead.FirstName = this.FirstName;
  return customLead;

An alternative, if the above proves to be too verbose, is to serialize the CustomPerson back to JSON, and then deserialize it but make sure you tell JSON.deserialize that the type should be CustomLead

share|improve this answer
Holy cow, that's genius.. I was really trying to avoid your first option - I actually have several more fields that I put in my post, so it does get to be very verbose very quickly... but serializing it back into JSON, then deserializing into the custom lead class is very simple (2 lines!). I just made that change and everything seems to be working now - thanks a bunch! – jongpie Aug 7 '14 at 15:29

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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