Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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 {
 //TODO
}

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

1 Answer 1

up vote 3 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 at 15:29

Your Answer

 
discard

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.