Skip to content

Instantly share code, notes, and snippets.

@tyoshikawa1106
Created February 13, 2015 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tyoshikawa1106/d31349136c53a58b83b1 to your computer and use it in GitHub Desktop.
Save tyoshikawa1106/d31349136c53a58b83b1 to your computer and use it in GitHub Desktop.
AddressクラスとLocationクラスの使い方
<apex:page controller="AddressAndLocationDemoController" showHeader="true" sidebar="false" id="page">
<div id="vf-page">
<apex:form id="form">
<!-- Address Block -->
<apex:pageBlock title="{!$ObjectType.Account.Label}" id="block">
<apex:pageBlockTable value="{!wrapperList}" var="item">
<apex:column headerValue="{!$ObjectType.Account.Fields.Name.Label}">
<apex:outputField value="{!item.account.Name}" />
</apex:column>
<apex:column headerValue="{!$ObjectType.Account.Fields.BillingCountry.Label}">
<apex:outputText value="{!item.acctCountry}" />
</apex:column>
<apex:column headerValue="{!$ObjectType.Account.Fields.BillingPostalCode.Label}">
<apex:outputText value="{!item.acctZipCode}" />
</apex:column>
<apex:column headerValue="{!$ObjectType.Account.Fields.BillingState.Label}">
<apex:outputText value="{!item.acctState}" />
</apex:column>
<apex:column headerValue="{!$ObjectType.Account.Fields.BillingCity.Label}">
<apex:outputText value="{!item.acctCity}" />
</apex:column>
<apex:column headerValue="{!$ObjectType.Account.Fields.BillingStreet.Label}">
<apex:outputText value="{!item.acctStreet}" />
</apex:column>
<apex:column headerValue="{!$ObjectType.Account.Fields.MyLocation__c.Label}">
<apex:outputText value="{!item.lat},{!item.lon}" />
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</div>
</apex:page>
@isTest
private class AddressAndLocationDemoControllerTest {
private static User testAdminUser = CommonTester.getLoginUser();
/**
* AddressAndLocationDemoControllerTest1
*/
static testMethod void AddressAndLocationDemoControllerTest1() {
System.runAs(testAdminUser) {
Test.startTest();
// Test
AddressAndLocationDemoController cls = new AddressAndLocationDemoController();
Test.stopTest();
// Result
System.assertEquals(cls.wrapperList.isEmpty(), true);
}
}
/**
* AddressAndLocationDemoControllerTest2
*/
static testMethod void AddressAndLocationDemoControllerTest2() {
System.runAs(testAdminUser) {
// Create Account
Account account = new Account(
Name = 'Test Account'
,BillingCountry = '日本'
,BillingPostalCode = '1xx-xxxxx'
,BillingState = '東京都'
,BillingCity = '千代田区'
,BillingStreet = '1-1-1'
,MyLocation__Latitude__s = 35.681
,MyLocation__Longitude__s = 139.764
);
insert account;
Test.startTest();
// Test
AddressAndLocationDemoController cls = new AddressAndLocationDemoController();
Test.stopTest();
// Result
System.assertEquals(cls.wrapperList.size(), 1);
System.assertEquals(cls.wrapperList[0].account.Name, account.Name);
System.assertEquals(cls.wrapperList[0].acctCountry, account.BillingCountry);
System.assertEquals(cls.wrapperList[0].acctZipCode, account.BillingPostalCode);
System.assertEquals(cls.wrapperList[0].acctState, account.BillingState);
System.assertEquals(cls.wrapperList[0].acctCity, account.BillingCity);
System.assertEquals(cls.wrapperList[0].acctStreet, account.BillingStreet);
System.assertEquals(cls.wrapperList[0].lat, account.MyLocation__Latitude__s);
System.assertEquals(cls.wrapperList[0].lon, account.MyLocation__Longitude__s);
}
}
/**
* AddressAndLocationDemoControllerTest3
*/
static testMethod void AddressAndLocationDemoControllerTest3() {
System.runAs(testAdminUser) {
// Create Account
Account account = new Account(
Name = 'Test Account'
);
insert account;
Test.startTest();
// Test
AddressAndLocationDemoController cls = new AddressAndLocationDemoController();
Test.stopTest();
// Result
System.assertEquals(cls.wrapperList.size(), 1);
System.assertEquals(cls.wrapperList[0].account.Name, account.Name);
System.assertEquals(cls.wrapperList[0].acctCountry, null);
System.assertEquals(cls.wrapperList[0].acctZipCode, null);
System.assertEquals(cls.wrapperList[0].acctState, null);
System.assertEquals(cls.wrapperList[0].acctCity, null);
System.assertEquals(cls.wrapperList[0].acctStreet, null);
System.assertEquals(cls.wrapperList[0].lat, null);
System.assertEquals(cls.wrapperList[0].lon, null);
}
}
}
public class AddressAndLocationDemoController {
public List<Wrapper> wrapperList {get; set;}
public AddressAndLocationDemoController() {
this.wrapperList = new List<Wrapper>();
List<Account> accounts = getAccounts();
for (Account account : accounts) {
Address addr = (account.BillingAddress != null) ? account.BillingAddress : new Address();
Location loc = (account.MyLocation__c != null) ? account.MyLocation__c : new Location();
this.wrapperList.add(new Wrapper(account, addr, loc));
}
}
private List<Account> getAccounts() {
return [SELECT Id, Name, BillingAddress,MyLocation__c FROM Account ORDER BY Name ASC LIMIT 10];
}
public class Wrapper {
public Account account {get; set;}
public String acctCountry {get; set;}
public String acctZipCode {get; set;}
public String acctState {get; set;}
public String acctCity {get; set;}
public String acctStreet {get; set;}
public Double lat {get; set;}
public Double lon {get; set;}
public Wrapper(Account account, Address addr, Location loc) {
this.account = account;
this.acctCountry = addr.Country;
this.acctZipCode = addr.PostalCode;
this.acctState = addr.State;
this.acctCity = addr.City;
this.acctStreet = addr.Street;
this.lat = loc.latitude;
this.lon = loc.longitude;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment