Iterate over a few objects in a single loop?
Clash 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
apex sobject
add a comment |Â
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
apex sobject
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
UsegetSObjectType()
method ofSObject
class (developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…)
– Pragati Jain
6 hours ago
add a comment |Â
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
apex sobject
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
apex sobject
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
UsegetSObjectType()
method ofSObject
class (developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…)
– Pragati Jain
6 hours ago
add a comment |Â
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
UsegetSObjectType()
method ofSObject
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
add a comment |Â
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
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
add a comment |Â
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
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
add a comment |Â
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
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
add a comment |Â
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
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
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
add a comment |Â
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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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 ofSObject
class (developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…)– Pragati Jain
6 hours ago