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
chandra2ravichandra2ravi 

Dynamic ObjectName

Hi All,

 

 I create one button on Contact(example) it send to Dynamic SOQL vfpage, but i am creating this button on every object. how to send  object name dynamically through url or any other stuff.

in controller i am using dynamic SOQL.

Example:

window.location ='/apex/DynamicSOQL?type={!$SObject}';

 

Regards,

Ravi.

Best Answer chosen by Admin (Salesforce Developers) 
Imran MohammedImran Mohammed

As you said that you have created separate button for each object, then the code posted should work fine.

 

For ex: suppose you have created separate button for Account , Opportunity etc.

In the javacript code for the button on Account you should have the URL as,

window.location ='/apex/DynamicSOQL?type={!$SObject.Account}';

For the button on Opportunity, the URL in javascript should be 

window.location ='/apex/DynamicSOQL?type={!$SObject.Opportunity}';

 

Let me know if you still have queries.

All Answers

super developersuper developer

Hi

Try this

 

'/apex/DynamicSOQL?type=?'+{!$ObjectType.book__c}

Imran MohammedImran Mohammed

Hi,

 

In your custom button code, select the object you have created the button into.

window.location ='/apex/DynamicSOQL?type={!$SObject.<<CurrentObject>>}';

This statement ({!$SObject.<<CurrentObject>>}) will return 3 characters which is the prefix of object.


 

In the Apex class,

  Get the qyery parameter, then 

  1.  Issue a Schema.getGlobaldescribe() call that returns a map.
  2. For each SObjectType, issue DescribeSobjectResult call
  3. There is method called keyPrefix which you have to compare it with the string you passed in the URL.
For ex: 
Map<String, Schema.SObjectType> gdMap = Schema.getGlobalDescribe();
for(SOBjectType so: gdMap.values())
{
Schema.DescribeSobjectResult  sdr = so.getDescribe();
if(sdr.getKeyPrefix() == <<value you got from URL query string>>)
        {
//get the name of the object
sdr.getName();
}
}
Hope this helps.
Let me know if you have any queries.

 

chandra2ravichandra2ravi

Hi Imaran,

 

i want to get  CurrentObject is dynamically.

window.location ='/apex/DynamicSOQL?type={!$SObject.<<CurrentObject>>}';

Imran MohammedImran Mohammed

As you said that you have created separate button for each object, then the code posted should work fine.

 

For ex: suppose you have created separate button for Account , Opportunity etc.

In the javacript code for the button on Account you should have the URL as,

window.location ='/apex/DynamicSOQL?type={!$SObject.Account}';

For the button on Opportunity, the URL in javascript should be 

window.location ='/apex/DynamicSOQL?type={!$SObject.Opportunity}';

 

Let me know if you still have queries.

This was selected as the best answer
chandra2ravichandra2ravi

Hi Imran,

 

But user create button. in simply paste the code without any changes  any possibility.

 

like CurrentObjectName this is either account , contact, some other object. where i am placing the button that object name i want.

 window.location ='/apex/DynamicSOQL?type={!$SObject.CurrentObjectName}';

 

and in button javascript following error is coming.

 

Field $SObject.Account does not exist. Check spelling.

Imran MohammedImran Mohammed

Hi,

 

Replace {!$SObject.CurrentObjectN​ame} with {!$ObjectType.Account} 

Sorry till now i thought you are referring to {!$ObjectType.CurrentObjectName}

 

In the URL it should be

window.location ='/apex/DynamicSOQL?type={!$ObjectType.CurrentObjectN​ame}';

 

chandra2ravichandra2ravi

i want type is dynamically.

i want this dynamic CurrentObjectN​ame where i place button that object name

window.location ='/apex/DynamicSOQL?type={!$ObjectType.CurrentObjectN​ame}';

Imran MohammedImran Mohammed

Chandra,

 

From Javascript, the prefix of the object will be determined and will be sent to Apex controller.

To find the object name, you have to use the apex code which i posted in the thread.

chandra2ravichandra2ravi

my requirement is user create a custom button in any object(account,contact,Project__c.....etc) 

just copy and paste the JavaScript code on button. but he dont know the object api name. I want universal(global) code working in every object without any replacing existing script code . any possibility to do this.

 

any possibility to send CurrentObjectN​ame dynamically without hard coding($ObjectType.Account.....).

 

window.location ='/apex/DynamicSOQL?type={!$ObjectType.CurrentObjectN​ame}';