If you're intending your class to be fully generic and work with any kind of sObject, you'll need to declare the variables you wish to use as type sObject
(or List<sObject>
, Map<Id, sObject>
, and so on). Your types must be concrete at compile time; in Apex, you can't get a dynamic reference to a type and then declare a variable as that type.
So in returnIds()
, you'd do
public pageReference returnIds() {
// do something with the selected records
for (sObject record : setCon.getSelected())
{
}
}
You don't need to do any casting, because getSelected()
already returns a List<sObject>
.
As you operate on these values, you'll only be able to use properties and methods defined at the sObject
level, like get()
and put()
. If your sObjects are really Opportunities, you won't be able to do record.CloseDate
, but you can access field data with record.get('CloseDate')
. Just bear in mind that those dynamic methods return untyped Object
values, and you'll have to cast them.
You can, however, directly access the Id
member of an sObject
-typed variable, as all sObjects have that member.