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
DSimpsonDSimpson 

Chatter trigger to create new Idea record

Disclaimer: I'm a newbie at programming in Apex.

My company wants to use a Chatter group to allow employees and customers to submit and comment on new product ideas. I need to write a trigger so that when a new FeedItem is created on the New Ideas group in Chatter, it creates a new Idea record, linked to the FeedItem.

Does anyone have an example of creating a new Idea object each time a new Chatter post occurs?

This is what I have so far, and of course, I get an error just saying that Salesforce has been notified.


 trigger createNewIdeaTrigger on FeedItem (before insert) {
        for (FeedItem f: trigger.new)
        {
            Idea newIdea = new Idea(Categories = 'NewIdea', Status = 'New', Body = f.Body, CommunityId = '09a300000004gvG', Title = 'New Idea') ;
                f.parentId = newIdea.Id;
                newIdea.postID__c = f.Id;
               
                insert newIdea;
           
        }
    }

Best Answer chosen by Admin (Salesforce Developers) 
DSimpsonDSimpson

Apparently, the only issues I had were the following:

a) for some reason, it wouldn't let me run this trigger in the "before Insert" -- only "after insert".

b) The parent.id wasn't write-able.

 

It now works.

All Answers

DSimpsonDSimpson

Apparently, the only issues I had were the following:

a) for some reason, it wouldn't let me run this trigger in the "before Insert" -- only "after insert".

b) The parent.id wasn't write-able.

 

It now works.

This was selected as the best answer
Pim UijttewaalPim Uijttewaal

 

Hi,

This script doesn't work?  I changed the community name, and this is my script:

 

trigger createNewIdeaTrigger on FeedItem (after insert) {
for (FeedItem f: trigger.new)
{
Idea newIdea = new Idea(Categories = 'NewIdea', Status = 'New', Body = f.Body, CommunityId = '09ab00000005QN4', Title = 'New Idea') ;
f.parentId = newIdea.Id;
newIdea.postID__c = f.Id;

insert newIdea;

}
}

 

 

But it says:  

Error: Compile Error: Invalid field postID__c for SObject Idea at line 6 column 17

 

 

Thx