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
Ronen SlifkinRonen Slifkin 

Visualforce Page with dynamic standardcontroller

Hello all

lets take this for example for a vf page 

apex:page standardController="Contact" extensions="vf_ContactsUpdateFieldSetsController" showHeader="false" sidebar="false"

 I want to be able to set the standartController with a changing value so this page would be dynamic and serve various objects.... and not just Contact

I also need to be able to use : contact = (Contact)controller.getRecord(); ----------> in a dynamic way so I can still can get the values from the screen
 

is there a way to achive that ? maybe by using only the extensions ? but how can I get the values of the fields in the screen ?

 

Any help or direction would be appriciated

background behind that need:
I would like to display a fieldset on that page , but the fieldset can be once from Contact , or any other Object.

 

 

TIA
 

Best Answer chosen by Ronen Slifkin
AshlekhAshlekh
Hi,

You just need a controller and VFP.

1) In controller when you get the id of record you need to dyanamiclly find the object by the first three digit of id ( because first three digit is the keyprefix of object so you can find object by id )

2) After getting object you need to store the object record in sObject type.

3) For fields you need call describe call on object and there is a method in describecall by which you can get the all fieds of that object.

4) Store the field result in list and make a dyanamic query and use Database.query method to fetch the record and save in sobject type.

5) Use get method on sobject and pass the value of field which you get from descrobe call and iterate and pass value in get method to get the value of tha field form sobject.

http://www.salesforce.com/us/developer/docs/dbcom_apex250/Content/apex_methods_system_sobject_describe.htm

http://salesforcenow.blogspot.in/2012/08/salesforce-find-object-from-record-id.html

All Answers

Deepak Kumar ShyoranDeepak Kumar Shyoran
No Can't use the StandardControl with dynamic in Salesforce as Salesforce doesn't provide this features yet.
Ronen SlifkinRonen Slifkin

Thanks.... any other workarounds any one might think of ?

 

AshlekhAshlekh
Hi,

You just need a controller and VFP.

1) In controller when you get the id of record you need to dyanamiclly find the object by the first three digit of id ( because first three digit is the keyprefix of object so you can find object by id )

2) After getting object you need to store the object record in sObject type.

3) For fields you need call describe call on object and there is a method in describecall by which you can get the all fieds of that object.

4) Store the field result in list and make a dyanamic query and use Database.query method to fetch the record and save in sobject type.

5) Use get method on sobject and pass the value of field which you get from descrobe call and iterate and pass value in get method to get the value of tha field form sobject.

http://www.salesforce.com/us/developer/docs/dbcom_apex250/Content/apex_methods_system_sobject_describe.htm

http://salesforcenow.blogspot.in/2012/08/salesforce-find-object-from-record-id.html
This was selected as the best answer
Ronen SlifkinRonen Slifkin

Ashlekh Thank you very much for this...

Im starting to work on it ,

1)  managed to  find the objectName based on it's Id and all is ok
2) this I need some more explanation if you can

     In which type should I store it and how ? and will it be the connection between the VF page and controller so I can get the values from the screen fields ?

      if I understand correctly this should replace this line right ? :  contact = (Contact)controller.getRecord();

 

regarding 3,4,5 I think I will manage but first I guess 2 should be more clear for me If you can please have a small example

 

TIA again

Ronen SlifkinRonen Slifkin

a little progress...
I have managed to create Sobject from a string with a name of an object which created and empy object.

The problem that I can't figure out is

1) how to get the value of the inputfield (in the VF Page) and get it's value in the controller...


when it was with standardController="Contact" for example

I had  contact = (Contact)controller.getRecord();

and to get a value I used : contact.get('some field name api');

how can I do it now with Sobject ?

 

 

 

 

Ronen SlifkinRonen Slifkin
well solved at last... :) thank you all
Rajendra PatelRajendra Patel
How did you solve it?
Ronen SlifkinRonen Slifkin

I have used VF page with with only controller attribute for the apex:page tag (no standardController)

according to the Id that passed to the controller I found  The object Name , and than Instantiated it

 

public Sobject currentObject     { get; set;}    // The relevant object like Contact , etc...
In the constructor
// retrieving the name of the object correlated to some Id 
currentObjectName = getObjectNameById(YOUR_ID_HERE);

// creating Sobject from the name of the object.
currentObject = getNewSobject(currentObjectName);
those are the method I used
// Get object name from it's ID (first 3 letters characters)
public static string getObjectNameById(string parent_id)
{
      Schema.DescribeSObjectResult object_desc = null;
    
        // Search every object in the getGlobalDescribe() map to check key prefixes
        for( Schema.SObjectType t : Schema.getGlobalDescribe().values() ){
              Schema.DescribeSObjectResult descr = t.getDescribe();
       
              if (descr.getKeyPrefix() != null)
              {
                   // If the Id starts with this objects prefix, then we know the type
                   if( parent_id.startsWith( descr.getKeyPrefix() ))
                   {
                       object_desc = descr;
                        break;
                   }
             }
     }
}

 

public SObject getNewSobject(String t)
{
    // Call global describe to get the map of string to token.
    Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
  
    // Get the token for the sobject based on the type.
    Schema.SObjectType st = gd.get(t);
  
    // Instantiate the sobject from the token.
    Sobject s = st.newSobject();
  
    return s;
  }
hope this will be  a good start for you