+ Start a Discussion
SassbertoSassberto 

Passing derived classes to create()

I have created a class called WebLead which inherits sforce.Lead.

When I atttempt to pass the derived class to create() I get an error that 'WebLead was found, expected sforce.Lead'. I have tried explicitly casting WebLead to it's base class but I still get the same error.

Ideally I want to something do this:

class WebLead : Lead
{
public Save()
{
sObject[] obj = { this };

sforce.create(obj)
}
}

instead I have to do

class WebLead : Lead
{
public Save()
{
Lead myLead = new Lead();
myLead.FirstName = this.FirstName;
myLead.LastName = this.LastName;
//etc.. ad nauseum

sObject[] obj = { myLead };

sforce.create(obj)
}
}

Any ideas here?
SuperfellSuperfell
I think the problem is that in the code generated from the WSDL, the definition of sObject doesn't iclude the WebLead as one of the derived types (there are attributes on the sObject definition that indicate all the derived types, which the xml serializer infrastructure needs)
talanbtalanb
I ran into this as well, so I changed from an inheritance model to a composition model. For example, I have an Account class that has a property called SfAccount that is of type Account, and it's that object that is sent in all SFDC transactions.

In your case it would look something like this:
class WebLead
{
private Lead sfLead;
public Lead SfLead {
get { return sfLead; }
put { sfLead = value; }
}
public WebLead() {
SfLead = new Lead();
}

public Save()
{
sObject[] obj = { SfLead };

sforce.create(obj)
}
}
and from outside your object...

WebLead myLead = new WebLead();
myLead.SfLead.FirstName = firstName;
...

HTH

Todd Breiholz