I need help converting this method's parameter into an sobject, so it can accept two different types of objects

So currently the method is returning a lead activity record, but I want it to return a candidate record if the object is a contact, so I was thinking to add another condition to return something like return new LightningActivity(candidateRecord) if this isn't a RH_Lead__c object

public static LightningActivity fetchTestActivity(sobject leadRecord, sobject candidateRecord) {
    if(leadRecord == null && leadRecord.getSObjectType=='RH_Lead__c')
        system.debug('got a null');
        return fetchTestActivity();
    if(candidateRecord == null && candidateRecord.getSObjectType=='Contact')
        system.debug('got a null');
        return fetchTestActivity();
    return new LightningActivity(leadRecord);

And here's my lightningactivity.apxc

public LightningActivity(RH_Lead__c leadRecord){
    system.debug('HEYO WE\'VE GOT A LEAD RECORD! + ');
    this.RHLeadId = leadRecord.id;
    this.RHLeadName = leadRecord.Title__c;

    if(leadRecord.Matched_Company__c == null){
        Account matched = AccountService.getAccountByNamesForLead(leadRecord.Company_Name1__c, leadRecord.Company_Name__c);
        if(matched != null){
                system.debug('JACK: match is found');
                this.company = matched;
                this.company.id = matched.id;
                this.companyId = matched.id;
                this.companyName = matched.Name;
                this.companyPhone = matched.Phone;
                this.companyExtension = String.valueOf(matched.Extension__c);

                this.exactContactMatchConsole = true;
        } else if(leadRecord.Company_Name1__c != null) {
                system.debug('Deja: no match, D & B Company "default"');
                this.companyName = leadRecord.Company_Name1__c;
                this.validCompany = false;
                this.exactContactMatchConsole = false;
        } else if(leadRecord.Company_Name__c != null) {
                system.debug('Deja: no match, Company Name "default"');
                this.companyName = leadRecord.Company_Name__c;
                this.validCompany = false;
                this.exactContactMatchConsole = false;
    } else {
        system.debug('Deja: matched company was populated');
        this.company = leadRecord.Matched_Company__r;
        this.company.id = leadRecord.Matched_Company__r.id;
        this.companyId = leadRecord.Matched_Company__r.id;
        this.companyName = leadRecord.Matched_Company__r.Name;
        this.companyPhone = leadRecord.Matched_Company__r.Phone;
        this.companyExtension = String.valueOf(leadRecord.Matched_Company__r.Extension__c);

    // contact
    if(leadRecord.Contact_in_Company__c != null){
        this.contact = leadRecord.Contact_in_Company__r;
        this.contact.id = leadRecord.Contact_in_Company__c;
        this.contactId = leadRecord.Contact_in_Company__c;
        this.contactName = leadRecord.Contact_in_Company__r.Name__c;
        this.contactEmail = leadRecord.Contact_in_Company__r.Consolidated_Email__c;
        this.contactPhone = leadRecord.Contact_in_Company__r.Primary_Phone_Code__c + leadRecord.Contact_in_Company__r.Primary_Phone__c;

    if(leadRecord.Lead_Source__c == 'Job Boards'){
        this.category = 'Marketing';
        this.type = 'Job Board Lead/Ad Call';
    } else if(leadRecord.Lead_Source__c == 'Branch-generated'){
        this.category = 'Marketing';
        this.type = 'Branch Generated Lead';
     else if(leadRecord.Lead_Source__c == 'Predicted'){
        this.category = 'Marketing';
        this.type = 'Predicted';
    else if(leadRecord.Lead_Source__c == 'Chat' || leadRecord.Lead_Source__c == 'Online Job Order Form'){
        this.category = 'Marketing';
        this.type = 'Online Lead';

    // S-581734 Jack Sampson
    if(leadRecord.Priority__c == '0' || leadRecord.Priority__c == '1'){
        this.priority = 'High';
    } else if (leadRecord.Priority__c == '2'){
        this.priority = 'Normal';
    } else if (leadRecord.Priority__c == '3' || leadRecord.Priority__c == '4'){
        this.priority = 'Low';

public LightningActivity(Contact candidateRecord){

    system.debug('Deja: candidate was populated');
    if(candidateRecord != null){
        this.candidateId = candidateRecord.Id; 
        this.candidateName = candidateRecord.Name;
        this.candidateEmail = candidateRecord.Email;
  • 1
    Welcome to SFSE! Please take the tour and read How to Ask. There are some issues with your terminology (a method cannot be an SObject, I suspect you meant changing the parameter of your method into a generic SObject), but beyond that I don't know if you've provided enough information for people to be able to provide an accurate answer. The LightningActivity class (and especially the constructor(s)) is probably going to be important in a good answer here, so I would recommend including that class as well. You can add information to your question by making an edit.
    – Derek F
    Mar 14, 2019 at 20:07
  • Probably easier to have two methods with different signatures. You can use smaller methods to fill in the internal behavior while reusing code. Mar 15, 2019 at 14:06
  • Hi @DerekFI changed my wording and added my class thank you, and I have also made some edits to my code
    – Deja Bond
    Mar 15, 2019 at 14:24
  • 1
    Have you ever encountered this error "Variable does not exist: getSObjectType" and "Constructor not defined: [LightningActivity].<Constructor>(SObject)" I have made edits according to your previous post by creating two seperate methods @battery.cord
    – Deja Bond
    Mar 15, 2019 at 17:44
  • 1
    Please do not remove the content of your question posts. Removing content destroys the value of the post and answers for the community. Posts that are vandalized will be locked.
    – David Reed
    Jul 19, 2019 at 17:04

1 Answer 1


Variable does not exist sObjectType

candidateRecord.getSObjectType should be candidateRecord.getSObjectType(). Its a function so you need to specific which parameters your passing (even if its none).

You'll also need to modify your if, getSObjectType() returns a Schema.sObjectType token, not a string. From that, get the describe, then call getName. It should look like this:

if (leadRecord == null && leadRecord.getSObjectType().getDescribe().getName() == 'RH_Lead__c')

Constructor not defined: [LightningActivity].<Constructor>(SObject)

You have two constructors for LightningActivity:

  1. (Contact candidateRecord)
  2. (RH_Lead__c leadRecord)

Neither of these will explicitly accept a sObject, because it doesn't know the internal type of the sObject, and it doesn't want to assume its type. We can check and change its type, but the compiler isn't quite there.

Your else clause is causing your problem, since you pass along a sObject.

return new LightningActivity(leadRecord); // leadRecord is sObject 

Aside: I'm not really sure what calling fetchTestActivity recursively without providing parameters is really accomplishing, or if its legal code (since I don't see a no-arg fetch in your code).

To have a method with one parameter which accepts more than one object type, we need to ensure our code is typesafe.

To start with, we need a method which accepts only one sObject. This record can be either of our supported types. We can use some code to figure out its type and pass it along to other methods.

public static LightningActivity fetchTestActivity(sObject leadOrCandidate) {
    // Check and handle nulls first 
    if (leadOrCandidate != null) {
        // use instanceof to check the type of a given sObject 
        if (leadOrCandidate instanceof RH_Lead__c) {
            // "Casting" can be used to turn a sObject into a different type. The synax is simple, (SomeType)someVariable => TypedVariable 
            return new LightningActivity((RH_Lead__c)leadOrCandidate);
        } else if (leadOrCandidate instanceof Contact) {
            return new LightningActivity((Contact)leadOrCandidate);
        } else {
            // Make a custom exception to throw here, add messages to page, etc 
            throw new ... 
    } else {
        // Handle Nulls 
        return null; 

My comment suggested instead of using generic sObjects checked at runtime, was to use a pair of methods, with a single parameter of the accepted object type. An example is below.

public static LightningActivity fetchTestActivity(RH_Lead__c lead) {
    if (lead != null) {
        return new LightningActivity(lead);
    } else {
        return null; 

public static LightningActivity fetchTestActivity(Contact contact) {
    if (contact != null) {
        return new LightningActivity(contact);
    } else {
        return null; 

The code is a lot simplier, and handles type problems at compile time, not runtime, which in my experience helps reduce errors. Unsupported types will cause an error similiar to the one you reported, Constructor not defined.

  • why not switch instead of instanceOf
    – cropredy
    Mar 16, 2019 at 1:06
  • Couldn't remember the syntax, wasn't in developer console. Mar 18, 2019 at 13:42
  • Thank you, I ended up going with your suggestion @battery.cord
    – Deja Bond
    Mar 18, 2019 at 17:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .