19. Source Code
// 非同期で中間サーバへ類似QA取得リクエストを送信
public with sharing class chatter_stackoverflow {
@future(callout=true)
public static void request_similar(string sid, string instanceUrl, string object_json){
Http http_protocol = new Http();
HttpRequest http_request = new HttpRequest();
// Set the endpoint URL.
instanceUrl = EncodingUtil.urlEncode(instanceUrl, 'UTF-8');
sid = EncodingUtil.urlEncode(sid, 'UTF-8');
String endpoint = 'https://chatter-translate.herokuapp.com/chatter/api.php?service=stackoverflow&sid='
+ sid + '&instanceUrl=' + instanceUrl;
http_request.setEndPoint(endpoint);
// Set the HTTP verb to GET.
http_request.setMethod('POST');
// Set body
http_request.setBody(object_json);
// set callout timeout to 60sec(max)
http_request.setTimeout(60000);
// Send the HTTP request and get the response.
// The response is in JSON format.
if (!Test.isRunningTest()){
http_protocol.send(http_request);
}
}
}
23. Chatter REST API vs. Apex
例えばCollaborationGroupFeedまたはRecordFeedに投稿する場合:
Chatter REST API:
POST https://インスタンス.salesforce.com/services/data/v25.0/chatter/feeds/record/グルー
プIDまたはレコードID/feed-items
{
"body" : {
"messageSegments" : [
{
"type": "Text",
"text" : "テスト書き込み”
}
]
}
}
Apex:
feedItem fi = new feedItem(body='テスト書き込み', parentId='グループIDまたはレコードID');
insert fi;
24. Chatter REST API vs. Apex
例えばUserProfileFeedに投稿する場合:
Chatter REST API:
POST https://インスタンス.salesforce.com/services/data/v25.0/chatter/feeds/user-
profile/ユーザーID/feed-items
{
"body" : {
"messageSegments" : [
{
"type": "Text",
"text" : "テスト書き込み”
}
]
}
}
Apex:
feedItem fi = new feedItem(body='テスト書き込み', parentId='ユーザーID');
insert fi;
25. Chatter REST API vs. Apex
例えばFeedCommentに投稿する場合:
Chatter REST API:
POST https://インスタンス.salesforce.com/services/data/v25.0/chatter/feed-items/親Feed
のID/comments
{
"body" : {
"messageSegments" : [
{
"type": "Text",
"text" : "テスト書き込み”
}
]
}
}
Apex:
feedComment fc = new feedComment(commentBody='テスト書き込み', parentId='親FeedのID');
insert fc;
26. Chatter REST API vs. Apex
例えばNewsFeedを取得する場合:
Chatter REST API:
POST https://インスタンス.salesforce.com/services/data/v25.0/chatter/feeds/news/me/feed-items
Apex:
List<NewsFeed> newsFeedList = [select id, parentId, body from newsfeed];