function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
r.ur.u 

Chatterグループへの投稿

今日からApexの勉強を始めました。 初歩的な質問ですいません。 ApexでChatterのグループへ投稿するプログラムがわかりません。 String groupId = '1F9N0000000Cdpl'; FeedItem feed = new FeedItem(); feed.parentId = groupId; feed.body = 'テストグループへの書き込み'; insert feed; 何が悪いのでしょう。
Best Answer chosen by Admin (Salesforce Developers) 
Taiki YoshikawaTaiki Yoshikawa

おそらくparentId = groupId;の所がうまくいっていないのだと思います。

検索条件で変数を指定する場合はparentId =: groupId;のように ":" が必要になります。

 

それとChatterグループのIDは直接指定するのではなくSOQLで取得するようにした方が

いいと思います。

 

Chatterグループの情報はCollaborationGroupというオブジェクトから取得できました。

 

サンプル

// ChatterGroup名指定
String groupName = 'Force.com Developer';

// ChatterGroup取得
CollaborationGroup objCollaborationGroup = [select Id ,Name from CollaborationGroup where Name =: groupName limit 1];

// フィードのINSERT
FeedItem feed = new FeedItem();
feed.parentId = objCollaborationGroup.Id;
feed.body = 'テストグループへの書き込み';
insert feed; 

 

All Answers

Taiki YoshikawaTaiki Yoshikawa

おそらくparentId = groupId;の所がうまくいっていないのだと思います。

検索条件で変数を指定する場合はparentId =: groupId;のように ":" が必要になります。

 

それとChatterグループのIDは直接指定するのではなくSOQLで取得するようにした方が

いいと思います。

 

Chatterグループの情報はCollaborationGroupというオブジェクトから取得できました。

 

サンプル

// ChatterGroup名指定
String groupName = 'Force.com Developer';

// ChatterGroup取得
CollaborationGroup objCollaborationGroup = [select Id ,Name from CollaborationGroup where Name =: groupName limit 1];

// フィードのINSERT
FeedItem feed = new FeedItem();
feed.parentId = objCollaborationGroup.Id;
feed.body = 'テストグループへの書き込み';
insert feed; 

 

This was selected as the best answer
r.ur.u
Taiki様 ご回答有り難う御座います。 頂いたサンプルコードでグループへの投稿ができるようになりました。