Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers 2

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

share|improve this answer
1  
add comment

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>
share|improve this answer
1  
Wait, Markup is editable on the ApexPage object? When did that happen? –  drakored Feb 25 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 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 –  Andrew Fawcett Feb 25 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 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 at 0:21
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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