Trialforce – Create a custom signup using Force.com Sites

There does not seem to much documentation online about this so I thought I would post how you can allow either potential customers or internal employees can create a trial/demo org via a web signup page. This page is hosted on Salesforce using Force.com sites. When you create the Visualforce page and controller below – you will need to add them to a Force.com site (SF Help – Creating Force.com Sites)

Note: you can only do the following in a Trialforce Management Organisation. If you dont have one of these, you will need to request it through the Salesforce partner community (More Information Here).

The first step is to create the custom controller. I have also included the ability to define the template id in a custom setting to make it easy to update the template that is used to create the orgs.

I have hardcoded a number of values but these could be easily added as options in the visualforce page. You could also add further functionality such as allowing users to choose what template they would like e.g. different templates for different industries.

When you create a ‘SignupRequest’ record and insert it successfully – an org will automatically be created and the user will be emailed the login details (SignupRequest object fields and description)
In my example, I have just populated the required field and ShouldConnectToEnvHub – this means that you can see the org created in the Environment Hub in your Trialforce Management Organisation.

public without sharing class TrialforceAutomation{
    
    public String emailAddress {get;set;}
    public String firstName {get;set;}
    public String lastName {get;set;}
    public boolean disableSave {get;set;}
    
    public TrialforceAutomation(){
    
        disableSave = false;
    }
    
    public void createTrialOrg(){
    
        disableSave = true;
    
        CustomSiteSettings ds = getTrialSettings();
        ds.OrgNumber__c +=1;
        
        SignupRequest sr = new SignupRequest();
        sr.signupEmail = emailAddress;
        sr.username = emailAddress +'.Demo'+ Integer.valueOf(ds.OrgNumber__c);
        sr.country = 'GB';
        sr.company = 'Demo Org : '+ Integer.valueOf(ds.OrgNumber__c);
        sr.firstName = firstName;
        sr.lastName = lastName;
        sr.ShouldConnectToEnvHub = true;
        sr.templateId = ds.Current_Template_ID__c;

        try{
            insert sr;           
            upsert ds;
            
        }Catch(DMLException ex){
        
            System.debug('::: Error Creating Trial Org : '+ex);
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Error Creating Demo Org - Please Try Again'));
            disableSave = false;
        }
        
        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,'Creating Demo Org - Details will be emailed to you'));
    }
} 

When the form is submitted, I disable the button to prevent users clicking it multiple times as the usernames do have to be unique. To keep the username unique, I have stored the latest org number in a custom setting and simply add this to the email address (note: you need to enter a start number in the default org value after creating the custom setting).
The user will also get a message to let them know that the process was a success. After the org is generated – the user receives an email with the login details (there is no code for this as it is part of the Salesforce org creation process).

I have also included the ability to define the template id in a custom setting to make it easy to update the template that is used to create the orgs. In my example, the list custom setting is called CustomSiteSettings__c and it has a custom text field called Current_Template_ID__c – the org default value is used to store the template id.



    private String getTemplateID(){
    
        CustomSiteSettings__c ds = CustomSiteSettings__c.getOrgDefaults();
        return ds.Current_Template_ID__c;
    }


    private CustomSiteSettings__c getTrialSettings(){
    
        return CustomSiteSettings__c.getOrgDefaults();
        
    }


Now that we have the controller create, we just need to add the visualforce page that we can use in the force.com site.


<apex:page controller="TrialforceAutomation" showHeader="false">

    <apex:form id="demoOrgDetails">
        <apex:pageBlock >
        <apex:pageMessages />
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="Save" action="{!createTrialOrg}" disabled="{!disableSave}"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection columns="1" title="Digital Genius Demo Org Signup">
                <apex:inputText value="{!firstName}" label="First Name" required="true"/>
                <apex:inputText value="{!lastName}" label="Last Name" required="true"/>
                <apex:inputText value="{!emailAddress}" label="Email Address" required="true"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

As I mentioned at the beginning, this is a basic page but you could easily add more options to allow the user to choose more customisation options.
You can view the full code in this Github repo

Leave a Reply