Last active
December 16, 2015 04:19
-
-
Save tyoshikawa1106/5376613 to your computer and use it in GitHub Desktop.
Apex DynamicComponentSample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public with sharing class DynamicComponentDemoController { | |
public Account account {get; set;} | |
/* | |
* DynamicComponentDemoController | |
*/ | |
public DynamicComponentDemoController() { | |
this.account = new Account(); | |
} | |
/* | |
* getDynamicPageBlock | |
*/ | |
public Component.Apex.PageBlock getDynamicPageBlock() { | |
// PageBlock | |
Component.Apex.PageBlock dynamicPageBlock = getPageBlock(); | |
// Add PageBlockSecion | |
Component.Apex.PageBlockSection dynamicPageBlockSection = getPageBlockSection(); | |
dynamicPageBlock.childComponents.add(dynamicPageBlockSection); | |
// Add InputField | |
Component.Apex.InputField dynamicAccountName = getAccountName(); | |
dynamicPageBlockSection.childComponents.add(dynamicAccountName); | |
// Add InputField | |
Component.Apex.InputField dynamicAccountNumber = getAccountNumber(); | |
dynamicPageBlockSection.childComponents.add(dynamicAccountNumber); | |
// Add InputText | |
Component.Apex.InputText dynamicInputText = getAccountSite(); | |
dynamicPageBlockSection.childComponents.add(dynamicInputText); | |
// Add PageBlockButtons | |
Component.Apex.PageBlockButtons dynamicPageBlockButtons = getPageBlockButtons(); | |
dynamicPageBlock.childComponents.add(dynamicPageBlockButtons); | |
// Add CommandButton | |
Component.Apex.CommandButton saveButton = getCommandButton(); | |
dynamicPageBlockButtons.childComponents.add(saveButton); | |
return dynamicPageBlock; | |
} | |
/* | |
* getPageBlock | |
*/ | |
private Component.Apex.PageBlock getPageBlock() { | |
return new Component.Apex.PageBlock( | |
title = 'Account Block' | |
,id = 'AccountBlock_Id' | |
,mode = 'edit' | |
,tabStyle = 'Account' | |
); | |
} | |
/* | |
* getPageBlockSection | |
*/ | |
private Component.Apex.PageBlockSection getPageBlockSection() { | |
return new Component.Apex.PageBlockSection( | |
title = 'Account Section' | |
,id = 'AccountSection_Id' | |
,collapsible = false | |
); | |
} | |
/* | |
* getAccountName | |
*/ | |
private Component.Apex.InputField getAccountName() { | |
Component.Apex.InputField dynamicAccountName = new Component.Apex.InputField(); | |
dynamicAccountName.expressions.value = '{!account.Name}'; | |
dynamicAccountName.id = 'AccountName_Id'; | |
return dynamicAccountName; | |
} | |
/* | |
* getAccountNumber | |
*/ | |
private Component.Apex.InputField getAccountNumber() { | |
Component.Apex.InputField dynamicAccountNumber = new Component.Apex.InputField(); | |
dynamicAccountNumber.expressions.value = '{!account.AccountNumber}'; | |
dynamicAccountNumber.id = 'AccountNumber_Id'; | |
return dynamicAccountNumber; | |
} | |
/* | |
* getAccountSite | |
*/ | |
private Component.Apex.InputText getAccountSite() { | |
Component.Apex.InputText dynamicInputText = new Component.Apex.InputText(); | |
dynamicInputText.expressions.label = '{!$ObjectType.Account.Fields.Site.Label}'; | |
dynamicInputText.expressions.value = '{!account.Site}'; | |
dynamicInputText.id = 'AccountSite_Id'; | |
return dynamicInputText; | |
} | |
/* | |
* getPageBlockButtons | |
*/ | |
private Component.Apex.PageBlockButtons getPageBlockButtons() { | |
return new Component.Apex.PageBlockButtons( | |
location = 'bottom' | |
); | |
} | |
/* | |
* getCommandButton | |
*/ | |
private Component.Apex.CommandButton getCommandButton() { | |
Component.Apex.CommandButton saveButton = new Component.Apex.CommandButton(); | |
saveButton.value = 'Save'; | |
saveButton.expressions.action = '{!doSave}'; | |
Set<String> rereders = new Set<String>{'form'}; | |
saveButton.rerender = rereders; | |
return saveButton; | |
} | |
/* | |
* doSave | |
*/ | |
public void doSave() { | |
upsert this.Account; | |
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Save!! : ' + this.Account.Name)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@isTest | |
private class DynamicComponentDemoControllerTest { | |
/* | |
* DynamicComponentDemoControllerTest | |
*/ | |
static testMethod void DynamicComponentDemoControllerTest() { | |
DynamicComponentDemoController cls = new DynamicComponentDemoController(); | |
} | |
/* | |
* getDynamicPageBlockTest | |
*/ | |
static testMethod void getDynamicPageBlockTest() { | |
DynamicComponentDemoController cls = new DynamicComponentDemoController(); | |
cls.getDynamicPageBlock(); | |
} | |
/* | |
* doSaveTest | |
*/ | |
static testMethod void doSaveTest() { | |
DynamicComponentDemoController cls = new DynamicComponentDemoController(); | |
cls.account.Name = 'Account Name'; | |
cls.account.AccountNumber = '0000'; | |
cls.doSave(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<apex:page controller="DynamicComponentDemoController" id="page"> | |
<apex:sectionHeader title="Force.com" subTitle="Dynamic Component Sample" /> | |
<apex:form id="form"> | |
<apex:pageMessages id="msg"/> | |
<apex:dynamicComponent componentValue="{!DynamicPageBlock}"/> | |
</apex:form> | |
</apex:page> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment