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
NakataNakata 

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 !

AhmedPotAhmedPot

Hi,

 

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.
 
   if(){
       opp = (Opportunity)controller.getRecord();
   }
else if(){
       acc = (Account )controller.getRecord();
   }
}
You can give it a try and check out if it works fine.
:)
Thanks,
Ahmed
NakataNakata

Thanks AhmedPot for the suggestion !

 

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

 

 

stdController.getRecord().getsObjectType()==Account.sObjectType

 

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

 

 

 

NakataNakata

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

 

System.debug('Account.sObjectType=='+Account.sObjectType);

NakataNakata

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