Iterate over a few objects in a single loop?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












I have a few objects that have the same fields (referring to Article Types) such as: FAQ__kav, Tutorial__kav, Video__Kav



Is it possible to add all of these records to a single list (I guess of type Sobject) and then iterate over it? If so, what's the correct syntax?



Hoping to find something like this:



Faq__kav record1;
Tutorial__kav record2;
Video__kav record 3;

List<Sobject> kbList = new List<Sobject>();

kbliist.add(record1);
kblist.add(record2);

for(Sobject record : kbList){
if(Sobject is type of FAQ__kav)
do something...
else if (Sobject is type of Tutorial__kav)
do something else









share|improve this question























  • yes, we can iterate in this way.
    – Pragati Jain
    6 hours ago










  • tnx @PragatiJain, what's the name of the method to distinguish between sobjects? (if Sobject is type of...)
    – Json
    6 hours ago











  • Use getSObjectType() method of SObject class (developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…)
    – Pragati Jain
    6 hours ago
















up vote
1
down vote

favorite












I have a few objects that have the same fields (referring to Article Types) such as: FAQ__kav, Tutorial__kav, Video__Kav



Is it possible to add all of these records to a single list (I guess of type Sobject) and then iterate over it? If so, what's the correct syntax?



Hoping to find something like this:



Faq__kav record1;
Tutorial__kav record2;
Video__kav record 3;

List<Sobject> kbList = new List<Sobject>();

kbliist.add(record1);
kblist.add(record2);

for(Sobject record : kbList){
if(Sobject is type of FAQ__kav)
do something...
else if (Sobject is type of Tutorial__kav)
do something else









share|improve this question























  • yes, we can iterate in this way.
    – Pragati Jain
    6 hours ago










  • tnx @PragatiJain, what's the name of the method to distinguish between sobjects? (if Sobject is type of...)
    – Json
    6 hours ago











  • Use getSObjectType() method of SObject class (developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…)
    – Pragati Jain
    6 hours ago












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have a few objects that have the same fields (referring to Article Types) such as: FAQ__kav, Tutorial__kav, Video__Kav



Is it possible to add all of these records to a single list (I guess of type Sobject) and then iterate over it? If so, what's the correct syntax?



Hoping to find something like this:



Faq__kav record1;
Tutorial__kav record2;
Video__kav record 3;

List<Sobject> kbList = new List<Sobject>();

kbliist.add(record1);
kblist.add(record2);

for(Sobject record : kbList){
if(Sobject is type of FAQ__kav)
do something...
else if (Sobject is type of Tutorial__kav)
do something else









share|improve this question















I have a few objects that have the same fields (referring to Article Types) such as: FAQ__kav, Tutorial__kav, Video__Kav



Is it possible to add all of these records to a single list (I guess of type Sobject) and then iterate over it? If so, what's the correct syntax?



Hoping to find something like this:



Faq__kav record1;
Tutorial__kav record2;
Video__kav record 3;

List<Sobject> kbList = new List<Sobject>();

kbliist.add(record1);
kblist.add(record2);

for(Sobject record : kbList){
if(Sobject is type of FAQ__kav)
do something...
else if (Sobject is type of Tutorial__kav)
do something else






apex sobject






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 6 hours ago

























asked 6 hours ago









Json

379316




379316











  • yes, we can iterate in this way.
    – Pragati Jain
    6 hours ago










  • tnx @PragatiJain, what's the name of the method to distinguish between sobjects? (if Sobject is type of...)
    – Json
    6 hours ago











  • Use getSObjectType() method of SObject class (developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…)
    – Pragati Jain
    6 hours ago
















  • yes, we can iterate in this way.
    – Pragati Jain
    6 hours ago










  • tnx @PragatiJain, what's the name of the method to distinguish between sobjects? (if Sobject is type of...)
    – Json
    6 hours ago











  • Use getSObjectType() method of SObject class (developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…)
    – Pragati Jain
    6 hours ago















yes, we can iterate in this way.
– Pragati Jain
6 hours ago




yes, we can iterate in this way.
– Pragati Jain
6 hours ago












tnx @PragatiJain, what's the name of the method to distinguish between sobjects? (if Sobject is type of...)
– Json
6 hours ago





tnx @PragatiJain, what's the name of the method to distinguish between sobjects? (if Sobject is type of...)
– Json
6 hours ago













Use getSObjectType() method of SObject class (developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…)
– Pragati Jain
6 hours ago




Use getSObjectType() method of SObject class (developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…)
– Pragati Jain
6 hours ago










1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










You can use this dynamic access technique (all SObjects support map-like methods):



for (Sobject record : kbList) 
String stringField = (String) record.get('StringFieldApiName');
...



to avoid the type-specific if/elseif/else coding for the common fields.



If you do need type-specific logic, the relatively new switch is a good way to do that:



for (Sobject record : kbList) 
switch on record
when Faq__kav f
// f is a reference of type Faq__kav in this block

when Tutorial__kav t
// t is a reference of type Tutorial__kav in this block

when Video__kav v
// v is a reference of type Video__kav in this block








share|improve this answer




















  • I didn't know that switch is available in Apex, thanks @Keith C!
    – Json
    4 hours ago






  • 1




    @Json Yeah it's fairly new so you don't see many examples of it. Not sure if the class API version needs to be set to the latest or not - best to do that anyway if it is new code.
    – Keith C
    3 hours ago










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "459"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f236771%2fiterate-over-a-few-objects-in-a-single-loop%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










You can use this dynamic access technique (all SObjects support map-like methods):



for (Sobject record : kbList) 
String stringField = (String) record.get('StringFieldApiName');
...



to avoid the type-specific if/elseif/else coding for the common fields.



If you do need type-specific logic, the relatively new switch is a good way to do that:



for (Sobject record : kbList) 
switch on record
when Faq__kav f
// f is a reference of type Faq__kav in this block

when Tutorial__kav t
// t is a reference of type Tutorial__kav in this block

when Video__kav v
// v is a reference of type Video__kav in this block








share|improve this answer




















  • I didn't know that switch is available in Apex, thanks @Keith C!
    – Json
    4 hours ago






  • 1




    @Json Yeah it's fairly new so you don't see many examples of it. Not sure if the class API version needs to be set to the latest or not - best to do that anyway if it is new code.
    – Keith C
    3 hours ago














up vote
2
down vote



accepted










You can use this dynamic access technique (all SObjects support map-like methods):



for (Sobject record : kbList) 
String stringField = (String) record.get('StringFieldApiName');
...



to avoid the type-specific if/elseif/else coding for the common fields.



If you do need type-specific logic, the relatively new switch is a good way to do that:



for (Sobject record : kbList) 
switch on record
when Faq__kav f
// f is a reference of type Faq__kav in this block

when Tutorial__kav t
// t is a reference of type Tutorial__kav in this block

when Video__kav v
// v is a reference of type Video__kav in this block








share|improve this answer




















  • I didn't know that switch is available in Apex, thanks @Keith C!
    – Json
    4 hours ago






  • 1




    @Json Yeah it's fairly new so you don't see many examples of it. Not sure if the class API version needs to be set to the latest or not - best to do that anyway if it is new code.
    – Keith C
    3 hours ago












up vote
2
down vote



accepted







up vote
2
down vote



accepted






You can use this dynamic access technique (all SObjects support map-like methods):



for (Sobject record : kbList) 
String stringField = (String) record.get('StringFieldApiName');
...



to avoid the type-specific if/elseif/else coding for the common fields.



If you do need type-specific logic, the relatively new switch is a good way to do that:



for (Sobject record : kbList) 
switch on record
when Faq__kav f
// f is a reference of type Faq__kav in this block

when Tutorial__kav t
// t is a reference of type Tutorial__kav in this block

when Video__kav v
// v is a reference of type Video__kav in this block








share|improve this answer












You can use this dynamic access technique (all SObjects support map-like methods):



for (Sobject record : kbList) 
String stringField = (String) record.get('StringFieldApiName');
...



to avoid the type-specific if/elseif/else coding for the common fields.



If you do need type-specific logic, the relatively new switch is a good way to do that:



for (Sobject record : kbList) 
switch on record
when Faq__kav f
// f is a reference of type Faq__kav in this block

when Tutorial__kav t
// t is a reference of type Tutorial__kav in this block

when Video__kav v
// v is a reference of type Video__kav in this block









share|improve this answer












share|improve this answer



share|improve this answer










answered 4 hours ago









Keith C

91.8k1087193




91.8k1087193











  • I didn't know that switch is available in Apex, thanks @Keith C!
    – Json
    4 hours ago






  • 1




    @Json Yeah it's fairly new so you don't see many examples of it. Not sure if the class API version needs to be set to the latest or not - best to do that anyway if it is new code.
    – Keith C
    3 hours ago
















  • I didn't know that switch is available in Apex, thanks @Keith C!
    – Json
    4 hours ago






  • 1




    @Json Yeah it's fairly new so you don't see many examples of it. Not sure if the class API version needs to be set to the latest or not - best to do that anyway if it is new code.
    – Keith C
    3 hours ago















I didn't know that switch is available in Apex, thanks @Keith C!
– Json
4 hours ago




I didn't know that switch is available in Apex, thanks @Keith C!
– Json
4 hours ago




1




1




@Json Yeah it's fairly new so you don't see many examples of it. Not sure if the class API version needs to be set to the latest or not - best to do that anyway if it is new code.
– Keith C
3 hours ago




@Json Yeah it's fairly new so you don't see many examples of it. Not sure if the class API version needs to be set to the latest or not - best to do that anyway if it is new code.
– Keith C
3 hours ago

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f236771%2fiterate-over-a-few-objects-in-a-single-loop%23new-answer', 'question_page');

);

Post as a guest













































































Comments

Popular posts from this blog

What does second last employer means? [closed]

List of Gilmore Girls characters

Confectionery