Forked from tyoshikawa1106/01_BookTrigger.trigger
Last active
August 29, 2015 14:09
-
-
Save arufian/846d91ea0060c51f7f2b to your computer and use it in GitHub Desktop.
This file contains hidden or 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 BookTrigger on Book__c (after insert, after update) { | |
BookTriggerHandler handler = new BookTriggerHandler(); | |
if (Trigger.isBefore) { | |
// before trigger logic | |
} else if (Trigger.isAfter) { | |
if (Trigger.isInsert) { | |
// GoogleBooksAPIで書籍の情報を取得 | |
handler.setGoogleBooksInfo(Trigger.new); | |
} else if (Trigger.isUpdate) { | |
// GoogleBooksAPIで書籍の情報を取得 | |
handler.setGoogleBooksInfo(Trigger.new, Trigger.newMap, Trigger.oldMap); | |
} else if (Trigger.isDelete) { | |
// delete trigger | |
} else if (Trigger.isUnDelete) { | |
// undelete trigger | |
} | |
} | |
} |
This file contains hidden or 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 BookTriggerHandler { | |
private BookTriggerAction action = new BookTriggerAction(); | |
/** | |
* コンストラクタ | |
*/ | |
public BookTriggerHandler() { | |
} | |
/** | |
* GoogleBooksAPIで書籍の情報を取得 | |
*/ | |
public void setGoogleBooksInfo(List<Book__c> books) { | |
// 対象のBook ID | |
Set<Id> bookIds = this.action.getBookIds(books); | |
if (bookIds.isEmpty()) return; | |
// 非同期でGoogle Books APIの処理実行 | |
BookTriggerFuture.doCalloutGoogleBooksApi(bookIds); | |
} | |
/** | |
* GoogleBooksAPIで書籍の情報を取得 | |
*/ | |
public void setGoogleBooksInfo(List<Book__c> books, Map<Id, Book__c> newBooksMap, Map<Id, Book__c> oldBooksMap) { | |
// 対象のBook ID | |
Set<Id> bookIds = this.action.getBookIds(books, newBooksMap, oldBooksMap); | |
if (bookIds.isEmpty()) return; | |
// 非同期でGoogle Books APIの処理実行 | |
BookTriggerFuture.doCalloutGoogleBooksApi(bookIds); | |
} | |
} |
This file contains hidden or 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 BookTriggerAction { | |
/** | |
* コンストラクタ | |
*/ | |
public BookTriggerAction() { | |
} | |
/** | |
* Book ID取得 | |
*/ | |
public Set<Id> getBookIds(List<Book__c> books) { | |
Set<Id> bookIds = new Set<Id>(); | |
for (Book__c b : books) { | |
bookIds.add(b.Id); | |
} | |
return bookIds; | |
} | |
/** | |
* Book ID取得 | |
*/ | |
public Set<Id> getBookIds(List<Book__c> books, Map<Id, Book__c> newBookMap, Map<Id, Book__c> oldBookMap) { | |
Set<Id> bookIds = new Set<Id>(); | |
for (Book__c b : books) { | |
if (newBookMap.get(b.Id).Name != oldBookMap.get(b.Id).Name) { | |
bookIds.add(b.Id); | |
} | |
} | |
return bookIds; | |
} | |
} |
This file contains hidden or 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 BookTriggerDao { | |
/** | |
* IDを条件にBook情報を取得 | |
*/ | |
public List<Book__c> getBooks(Set<Id> bookIds) { | |
return [ | |
SELECT | |
Id | |
,Name | |
,Price__c | |
,Web_Results__c | |
FROM | |
Book__c | |
WHERE ID IN: bookIds | |
]; | |
} | |
} |
This file contains hidden or 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 BookTriggerFuture { | |
/** | |
* 非同期処理でGoogleBooksApiから書籍情報を取得 | |
*/ | |
@future (callout=true) | |
public static void doCalloutGoogleBooksApi(Set<Id> bookIds) { | |
BookTriggerAction action = new BookTriggerAction(); | |
BookTriggerDao dao = new BookTriggerDao(); | |
// IDを条件にBook情報取得 | |
List<Book__c> books = dao.getBooks(bookIds); | |
Map <String, Book__c> bookMap = new Map<String, Book__c>(); | |
// ※NameをKeyにするとユニークにならないので要検討... | |
for (Book__c b : Books) { | |
bookMap.put(b.Name, b); | |
} | |
// | |
// Google Books APIを使った処理...(省略) | |
// | |
// APIで取得したGoogleBooksの情報を条件にBook情報を更新 | |
List<Book__c> updateBooks = new List<Book__c>(); | |
for (String key : bookMap.keySet()) { | |
Book__c wkBook = bookMap.get(key); | |
wkBook.Price__c = 777777; // Priceに値をセット(サンプル) | |
updateBooks.add(wkBook); | |
} | |
update updateBooks; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment