5

I'm wondering if there's a way to create a visualforce page that lists all the objects (standard & custom) in a Salesforce org and each to next object there is a button called "Create Page" which, when pressed, will create a Visualforce page with that object's controller?

Or could this be possible with a set of Triggers?

3 Answers 3

10

Not natively - Apex and Visualforce do not have direct access to the MetaData API, so you would need to approach it similar to other API integrations. You might check out FinancialForce's apex-mdapi library: https://github.com/financialforcedev/apex-mdapi

1
9

You can knock this out fairly simply by using the Ajax Toolkit within your visualforce page, which is capable of creating and updating visualforce via create and update calls. Here is an example of a page that updates itself, recoding the name of whomever visits it:

<apex:page showHeader="true" sidebar="true" title="???">

    <apex:includeScript value="/soap/ajax/25.0/connection.js"/>
    <apex:includeScript value="/soap/ajax/25.0/apex.js"/>

    <div>
        Who has visited this page so far:<br /><br />
        WHOSE NEXT?
    </div>

    <script>
        sforce.connection.sessionId = '{!$Api.Session_ID}';

        function printUsername(result) {
            result         = result.getArray('records');
            var next       = 'WHOSE NEXT?';
            var newName    = '{!$User.FirstName & " " & $User.LastName} <br/>' + next;

            result[0].Markup = result[0].Markup.replace(next, newName);
            sforce.connection.update(result);
        }

        sforce.connection.query(
            "SELECT Id, Markup FROM ApexPage WHERE Name = 'Mystery'", 
            { onSuccess : printUsername });
    </script>

</apex:page>
6
  • 1
    Wait, Markup is editable on the ApexPage object? When did that happen?
    – drakored
    Feb 25, 2014 at 23:39
  • 1
    Been that way for years. Just have to do it externally from salesforce (read: not from apex). SOQL Xplorer can be a visualforce ide if you really wanted it to. Works for Visualforce and Static resources, but no dice for Apex.
    – Phil Rymek
    Feb 25, 2014 at 23:41
  • 1
    Which means this will probably work via SF REST API from Apex also I guess, nice. Spring'14 also brings with it a Metadata API update API that is synchronous Feb 25, 2014 at 23:46
  • @PhilRymek - Nice, I seem to recall trying from anon apex, I bet that is why I assumed it was unwritable.
    – drakored
    Feb 26, 2014 at 0:01
  • Thanks Phil! You mention it can update and create? I'm really looking for the ability to have a user select a subset of their Objects (standard and custom) for which they want a VF page created - would this functionality be able to then create those VF pages?
    – McD
    Feb 27, 2014 at 0:21
0

If you want complete flexibility, you will have to go for metadata api. There is metadata API wrapper classes developed by financial force team. Details can be found in below link,

https://github.com/financialforcedev/apex-mdapi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.