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
Leafen SandyLeafen Sandy 

Get the list of available object names within javascript

Hi,

I have a page where I have to show the list of all objects in a drop down menu.

But the challenge here is that I'm not using any controller, so I have to do query the objects in the page possibly within the javascript.

Or is there any other simpler way that I can bring out the names of all available objects....

Thanks,
Leafen.
NagendraNagendra (Salesforce Developers) 
Hi Sandy,

If you want to display picklist with any sObjects or fields in your visual force page. you can do it using javascript without using Apex controller. No need to write a class.

Please find the sample code below and tweak it as per your requirement which should help you.
Visualforce Page:
<apex:page>
    <apex:includeScript value="/soap/ajax/16.0/connection.js"/>
    <label id="ldfield" style="font-weight:bold">Account Fields: </label>
    <select id="selectNumber">
    </select>
     <script>
        sforce.connection.sessionId = '{!$Api.Session_ID}'; 
        //Global Object
        var describeResults = sforce.connection.describeSObject("Account");
        //select option list
        var select = document.getElementById("selectNumber");
        for(var i = 0; i < describeResults.fields.length; i++) { 
            var fieldList = describeResults.fields[i];      
            var el = document.createElement("option");
            el.textContent = fieldList.label;
            el.value = fieldList.Name;
            select.appendChild(el);        
        }
    </script>
</apex:page>
Thanks,
Nagendra
 
Leafen SandyLeafen Sandy
Hi Nagendra,

Thanks for your immediate response.

It looks like I have to make myself even more clear.

The sample code which you have given is to get the field labels of any particular object(in this example its Account). But I just need the names of all available objects (standard and custom) as the picklist value.

Hope this time I made my question simple and clear.

Thanks Again,
Leafen.
 
Javwad AzeemJavwad Azeem
Hi Leafen,

Here is the sample code to display the list of all objects:-
 
public class mycontroller {
	public string currentobject { get; set; }
	public string currentfield { get; set; }
	
	public selectoption[] getobjects() {
		selectoption[] objects = new selectoption[0];
		map<string,schema.sobjecttype> describe = schema.getglobaldescribe();
		for(string objectname:describe.keyset()) {
			objects.add(new selectoption(objectname,describe.get(objectname).getdescribe().getname()));
		}
		return objects;
	}
	
	public selectoption[] getfields() {
		selectoption[] fields = new selectoption[0];
		map<string,schema.sobjecttype> describe = schema.getglobaldescribe();
		if(describe.containskey(currentobject)) {
			map<string,schema.sobjectfield> fieldmap = describe.get(currentobject).getdescribe().fields.getmap();
			for(string fieldname:fieldmap.keyset()) {
				fields.add(new selectoption(fieldname,fieldmap.get(fieldname).getdescribe().getlabel()));
			}
		}
		return fields;
	}
}

Please refer the below links for more details:-
  • https://salesforce.stackexchange.com/questions/134255/how-to-get-the-standard-and-custom-object-api-name-field-api-name-data-type-fo
  • https://jungleeforce.wordpress.com/2013/06/07/extracting-list-of-all-the-objects-standard-custom-in-salesforce/
Hope it will help:-

Regards,
Javwad Azeem
Leafen SandyLeafen Sandy
Hi Javwad,

Thanks for your response.

My first most challenge is to do every thing within the page, i.e., I should not go for a controller.

So kindly help me out to do with javascript or any such thing negotiating the usage of a controller.

Thanks,
Leafen.