0

Refactoring code to merge similar operations and getting an error the SObjects I'm passing to this method are variables that do not exist.

private static List<SObject> getSObjectList(SObject salesforceObject, Set<String> filterSet, String exceptionMessage) {
  List<SObject> records;
  switch on salesforceObject {
    when Contact {
      records = [SELECT Id, Name FROM Contact WHERE Name IN :filterSet];
    }
    when pse__Milestone__c {
      records = [SELECT Id, Milestone_ID__c, pse__Project__c, pse__Project__r.pse__Account__c, pse__Project__r.pse__Allow_Timecards_Without_Assignment__c, pse__Project__r.pse__Is_Billable__c, pse__Project__r.Apply_Burndown_Cap__c, pse__Project__r.Remaining_Hours__c, Exclude_Timecards_from_Billing__c FROM pse__Milestone__c WHERE Milestone_ID__c IN :filterSet];
    }
  }

  if (records.size() == 0) {
    notifyAdmin('TimeLoaderJira Exception', exceptionMessage);
    throw new ImportException(exceptionMessage);
  }

  return records;
}

Getting error "Variable does not exist: Contact" from this line that invokes it:

List<Contact> resources = (List<Contact>) getSObjectList(Contact, resourceNames, 'Data imported did not contain any valid resources.');

and "Variable does not exist: pse__Milestone__c" for this one:

List<pse__Milestone__c> milestones = (List<pse__Milestone__c>) getSObjectList(pse__Milestone__c, milestonesFriendly, 'Data imported did not contain any valid milestones.');

Any thoughts about what I'm doing wrong?

0

1 Answer 1

3

You need to create an actual sobject:

List<pse__Milestone__c> milestones = (List<pse__Milestone__c>) getSObjectList(
  new pse__Milestone__c(), 
  milestonesFriendly, 
  'Data imported did not contain any valid milestones.'
);

Or, perhaps you actually meant to use sObjectType in your method parameter (this would have made more sense):

private static List<SObject> getSObjectList(SObjectType salesforceObject, Set<String> filterSet, String exceptionMessage) {
  List<SObject> records;
  switch on salesforceObject.newSObject() {
    when Contact {
      records = [SELECT Id, Name FROM Contact WHERE Name IN :filterSet];
    }
    when pse__Milestone__c {
      records = [SELECT Id, Milestone_ID__c, pse__Project__c, pse__Project__r.pse__Account__c, pse__Project__r.pse__Allow_Timecards_Without_Assignment__c, pse__Project__r.pse__Is_Billable__c, pse__Project__r.Apply_Burndown_Cap__c, pse__Project__r.Remaining_Hours__c, Exclude_Timecards_from_Billing__c FROM pse__Milestone__c WHERE Milestone_ID__c IN :filterSet];
    }
  }

  if (records.size() == 0) {
    notifyAdmin('TimeLoaderJira Exception', exceptionMessage);
    throw new ImportException(exceptionMessage);
  }

  return records;
}

Which would be called as:

List<pse__Milestone__c> milestones = (List<pse__Milestone__c>) getSObjectList(
  pse__Milestone__c.sObjectType, 
  milestonesFriendly, 
  'Data imported did not contain any valid milestones.'
);

You must log in to answer this question.

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