Created
November 16, 2010 12:45
-
-
Save stomita/701776 to your computer and use it in GitHub Desktop.
AccountAddressTrigger.trigger
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
trigger AccountAddressTrigger on Account (before insert, before update) { | |
Pattern p = Pattern.compile('^\\s*〒?\\s*(\\d{3}\\-\\d{4})?\\s*(東京都|北海道|(?:京都|大阪)府|.{2,3}県)?\\s*(.[^市区町村]*[市区群町村])(.*\\s*.*)$'); | |
for (Account acc : Trigger.new) { | |
if (Trigger.isUpdate) { | |
Account oldAcc = Trigger.oldMap.get(acc.Id); | |
if (oldAcc.Address__c == acc.Address__c) continue; | |
} | |
if (acc.Address__c == null) continue; | |
Matcher m = p.matcher(acc.Address__c); | |
if (m.find()) { | |
acc.BillingPostalCode = m.group(1); | |
acc.BillingState = m.group(2); | |
acc.BillingCity = m.group(3); | |
acc.BillingStreet = m.group(4); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment