+ Start a Discussion
PeacePeace 

Fetch few standard objects and all custom objects name

Hi,

 

Iam new to salesforce. I have a simple query.

 

Below code returns all the custom and standard objects.

 

How should i modify it,  so that it returns all the custom objects and only three standard object (viz.  Accounts, Leads and Contacts)

 

 public List<SelectOption> getItems()

    {
    for ( Schema.SObjectType typ : Schema.getGlobalDescribe().values() )
    {
        String sobjName = String.valueOf(typ);
        if ( sobjName.contains('__c')  || (!sobjName.contains('__c')))
        {
            stdObjectNames.add(sobjName);
            options1.add(new SelectOption(sobjName,sobjName));
           
        }
    }
    
       return options1;

     }

  If i manually add object as below, then it won't fetch actual "Accounts"  object details

     options1.add(new SelectOption('Accounts','Accounts'));

Please help..

Best Answer chosen by Admin (Salesforce Developers) 
PeacePeace

Hi Puja,

 

I tried your solution.  However, its only returning Custom objects.

Please suggest some changes so that i can get both custom and 3 standard objects.

 

Thanks in advnce

All Answers

Puja_mfsiPuja_mfsi

Hi,

Use this.declare a set which contains the standard object name and check in the inside the for loop.

 

Set<String> stdObjectSet = new Set<String>{'Account','Contact','Lead'};
List<SelectOption> options1 = new List<SelectOption>();
for ( Schema.SObjectType f : Schema.getGlobalDescribe().values()){
if ( f.getDescribe().isCustom() || stdObjectSet.contains(f.getDescribe().getName()) ){
options1.add(new SelectOption(f.getDescribe().getName(),f.getDescribe().getLabel()));
}
}

 

And use the Schema.SObjectType standard method to retreive the label name,Api Name 

Please refer the below link:

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_sobject_describe.htm

PeacePeace

Hi Puja,

 

I tried your solution.  However, its only returning Custom objects.

Please suggest some changes so that i can get both custom and 3 standard objects.

 

Thanks in advnce

This was selected as the best answer
Puja_mfsiPuja_mfsi

Hi,

You need to create a set 

Set<String> stdObjectSet = new Set<String>{'Account','Contact','Lead'};

and then check as follow :

 

for ( Schema.SObjectType f : Schema.getGlobalDescribe().values()){
    if ( f.getDescribe().isCustom() || stdObjectSet.contains(f.getDescribe().getName()) ){
                     options1.add(new SelectOption(f.getDescribe().getName(),f.getDescribe().getLabel()));
    }

}

 

 

if stdObjectSet.contains(f.getDescribe().getName()) in above If condition.

 

Or you can write like:

 

 if ( f.getDescribe().isCustom() || f.getDescribe().getName() == 'Account' ||  f.getDescribe().getName() == 'Contact' || f.getDescribe().getName() == 'Lead'){

                options1.add(new SelectOption(f.getDescribe().getName(),f.getDescribe().getLabel()));

}

PeacePeace

Thanks puja ,

 

your solution is correct.. by mistake I accepted wrong one as solution.

PeacePeace

Hi,

 

Is there any way to fetch few of the standard objects, without hardcoding names of the object (Set<String> stdObjectSet = new Set<String>{'Account','Contact','Lead'};)

Can we fetch only those standard objects which are usually displayed as tabs on home page such as Campaigns, Leads, Contacts, Accounts, Opportunities, Forecasts, etc.

Is it possible?

 

Please help