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

How can i dynamically detect what standard controller object using ?

Good day,


Anyone have idea how can i get the standard controller object name from my controller ? meaning if my  VF page have standardcontroller = "Account" , then in my controller will able to recognise Account as Standard controller object.


Reason of doing that is i have multiple page which have different standard controller in respective page but they have suppose in same behaviour.


ie : 



Mycontroller (ApexPages.StandardController sc){
// how can i know and get dynamically on standard controller object like "Account, Contact..." ?


Thanks in advance !




first 3 characters of your record id act as unique indentifier for objects

Also getKeyPrefix() method of sObject describe result returns to first 3 character indentifier for any object. You can do some comparision of 1st 3 characters of your record Id andhave some conditional statements in you constructor.


Note: for standard object, unique indentifier would be same but for custom objects it may vary. So you are btter of using getKeyPrefix() method and comparing with your record Ids.


for eg:

Opportunity opp;

Account acc;

Mycontroller (ApexPages.StandardController sc){
  get record id and retrieve first 3 characters.
       opp = (Opportunity)controller.getRecord();
else if(){
       acc = (Account )controller.getRecord();
You can give it a try and check out if it works fine.

Thanks AhmedPot for the suggestion !


at the moment, i trying something like below, believe this can be compare whether it is same SObject





But i got the error like "Invalid field sObjectType" you have idea ?





Strange thing is it work in System log but in IDE not




found the issue as it caused by object instance declared the same name :( working fine now