function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
aswanijagadesh1.397398535585991E12aswanijagadesh1.397398535585991E12 

Dispalying all objects in organization with its keyprefixs ?

Hai friends,
i want to display all objects with its keyprefix values for this i done like this

in controller class:
public class GettingObjectsAndKeyPrifix {
    public List<Schema.SObjectType> gd {get;set;}
    public List<String> objectMap {get;set;}

     Public GettingObjectsAndKeyPrifix(){
        gd= Schema.getGlobalDescribe().Values();
        objectMap = new List<String>();
        for(Schema.SObjectType f : gd)
        {
            objectMap.add(f.getDescribe().getKeyPrefix());
        }
        System.debug('--------- --'+gd);
        System.debug('+++++++++ --'+objectMap.size() );
    }
}

in visual force page:
<apex:page controller="GettingObjectsAndKeyPrifix">
    <!--http://srinivas4sfdc.blogspot.in/2013/12/list-of-salesforce-object-key-prefixes.html-->
   <apex:pageBlock >
   <apex:pageBlockSection >
        <apex:pageBlockTable value="{!objectMap}" var="o">
        <apex:column value="{!o}"></apex:column>
        </apex:pageBlockTable>  
      <apex:pageBlockTable value="{!gd}" var="g">
    <apex:column value="{!g}"></apex:column>
    </apex:pageBlockTable>
  
    </apex:pageBlockSection>
   
     </apex:pageBlock>
   
</apex:page>

here output is comming ,but i want output like

account--001
Note-002
like this how can i please help.

i want ouput like this in url
http://srinivas4sfdc.blogspot.in/2013/12/list-of-salesforce-object-key-prefixes.html
Best Answer chosen by aswanijagadesh1.397398535585991E12
Abhinav GuptaAbhinav Gupta
#1 Try passing list<Schema.SObjectType> to UI

I doubt it might fail because Schema.SObjectType might not be serialisable

#2 Create a wrapper class for capturing required details

Here is some code what you can take forward

Apex

public class gettingObjectsAndKeyPrefix {

 	public class SObjectInfo {
 		public String prefix {get;set;}
 		public String name {get;set;}


 	}

	public List<SObjectInfo> objectMap {get;set;}

    public GettingObjectsAndKeyPrifix(){
        gd= Schema.getGlobalDescribe().Values();
        objectMap = new List<String>();
        for(Schema.SObjectType f : gd)
        {
        	SObjectInfo si = new SObjectInfo();
        	si.prefix = f.getDescribe().getKeyPrefix();
        	si.name = f.getDescribe().getLabel();
            objectMap.add(si);
        }        
    }

}
Visualforce

<apex:pageBlockTable value="{!objectMap}" var="o">
    <apex:column headerValue="Key Prefix" value="{!o.prefix}"></apex:column>
    <apex:column headerValue="Object Type" value="{!o.name}"></apex:column>
</apex:pageBlockTable>



All Answers

Abhinav GuptaAbhinav Gupta
You need to change one line i.e.

objectMap.add(f.getDescribe().getLabel() + '--' + f.getDescribe().getKeyPrefix());

aswanijagadesh1.397398535585991E12aswanijagadesh1.397398535585991E12
hai Abhinav Gupta,
it is working fine,but i want output  in two columns not in single column

like this in this url
http://srinivas4sfdc.blogspot.in/2013/12/list-of-salesforce-object-key-prefixes.html

Abhinav GuptaAbhinav Gupta
#1 Try passing list<Schema.SObjectType> to UI

I doubt it might fail because Schema.SObjectType might not be serialisable

#2 Create a wrapper class for capturing required details

Here is some code what you can take forward

Apex

public class gettingObjectsAndKeyPrefix {

 	public class SObjectInfo {
 		public String prefix {get;set;}
 		public String name {get;set;}


 	}

	public List<SObjectInfo> objectMap {get;set;}

    public GettingObjectsAndKeyPrifix(){
        gd= Schema.getGlobalDescribe().Values();
        objectMap = new List<String>();
        for(Schema.SObjectType f : gd)
        {
        	SObjectInfo si = new SObjectInfo();
        	si.prefix = f.getDescribe().getKeyPrefix();
        	si.name = f.getDescribe().getLabel();
            objectMap.add(si);
        }        
    }

}
Visualforce

<apex:pageBlockTable value="{!objectMap}" var="o">
    <apex:column headerValue="Key Prefix" value="{!o.prefix}"></apex:column>
    <apex:column headerValue="Object Type" value="{!o.name}"></apex:column>
</apex:pageBlockTable>



This was selected as the best answer
aswanijagadesh1.397398535585991E12aswanijagadesh1.397398535585991E12

it gives an error Error: Compile Error: Illegal assignment from LIST<String> to LIST<GettingObjectsAndKeyPrifix.SObjectInfo> at line 13 column 9

like this i wrote:

 public class GettingObjectsAndKeyPrifix{
    public List<Schema.SObjectType> gd {get;set;}
    public class SObjectInfo {
        public string prefix {get;set;}
        public string name {get;set;}
    }

    public List<SObjectInfo> objectMap {get;set;}

    public GettingObjectsAndKeyPrifix(){

        gd= Schema.getGlobalDescribe().Values();
        objectMap = new List<String>();

        for(Schema.SObjectType f : gd)
 
        {
         SObjectInfo si = new SObjectInfo();
          si.prefix = f.getDescribe().getKeyPrefix();
          si.name = f.getDescribe().getLabel();
          objectMap.add(si);
       }        
  }

}

will you please tell me ,in that format we will get the output or not

aswanijagadesh1.397398535585991E12aswanijagadesh1.397398535585991E12
sorry , chanhe this :objectMap=new List<SObjectInfo>();
it is working thank you so much.
 
Sumit Saini 1Sumit Saini 1
Hi Friends,

Here are the source code for getting any sObject key prefix.

https://sfdctechsolutions.blogspot.com/2021/07/get-sobject-prefix.html

Please review this.
JustAnotherAdminJustAnotherAdmin
This is also achievable with SOQL (in my case has been useful for downloading into a spreadsheet or something similar)
 
SELECT Id, Label, DeveloperName, QualifiedApiName, KeyPrefix, NamespacePrefix, Publisher.Name
FROM EntityDefinition
ORDER BY QualifiedApiName ASC
LIMIT 2000 OFFSET 0

Caveats: 
•    does not support queryMore which means the largest single pull can be 2000 (reason for the LIMIT statement above)
•    overcoming the queryMore limitation can be achieved by simply pulling multiple times (reason for the OFFSET statement above)
LIMIT 2000 OFFSET 2000 (means pull 2000 and start at 2000 = would result in records 2000-4000 from your Org)
[my experience is less than 10k records for some well sized Enterprise Orgs which would equate to 5x pulling this query and adjusting the offset]

Alternatively:
filter resultset using a 'where' clause like below, preferrably something that limits resultset to fewer than 2000 records
i.e. WHERE QualifiedApiName LIKE '%[enter object name or part here]%'