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
akallioWileyakallioWiley 

A Simple Chatter Trigger

Hi All. Below is a really simple chatter trigger that I have written to help me with creating cases from chatter posts. It's my first chatter trigger, and I'm getting stumped on the line that is commented out. I can't get that line to compile. It seems that I can't get the FeedItem's Body from the feedcomment. Is my syntax wrong, or is not possible this way?

 

Thanks!

 

 

 

trigger createCaseFromChatter on FeedComment (after insert) {

    for(FeedComment f : trigger.new) {
        if(f.CommentBody.startsWith('!newCase')) {
            Case newCase = new Case(
            	Subject = f.CommentBody,
                //Description = f.FeedItem.Body,
                Origin = 'Chatter',
                RecordtypeId = '012G0000000yArc',
                Status = 'Open'                
            );
            insert newCase;
        }
    }
}

 

Best Answer chosen by Admin (Salesforce Developers) 
Naidu PothiniNaidu Pothini
trigger createCaseFromChatter on FeedComment (after insert)
{
    List<Case> caseList = new List<Case>();

    for(FeedComment f : trigger.new)
    {
        if(f.CommentBody.startsWith('!newCase'))
        {
        	feedItemIds.add(f.FeedItemId);
        }
    }

    Map<Id, FeedItem> feedItemMap = new Map<Id, FeedItem>([SELECT Id, Body FROM FeedItem WHERE Id IN :feedItemIds]);

    for(FeedComment f : trigger.new)
    {
        if(f.CommentBody.startsWith('!newCase'))
        {
            Case newCase = new Case(Subject = f.CommentBody,
            						Description = feedItemMap.get(f.FeedItemId).Body,
					                Origin = 'Chatter',
					                RecordtypeId = '012G0000000yArc',
					                Status = 'Open');
            caseList.add(newCase);
        }
    }

    insert caseList;
}

 try this.

All Answers

Naidu PothiniNaidu Pothini
trigger createCaseFromChatter on FeedComment (after insert)
{
    List<Case> caseList = new List<Case>();

    for(FeedComment f : trigger.new)
    {
        if(f.CommentBody.startsWith('!newCase'))
        {
        	feedItemIds.add(f.FeedItemId);
        }
    }

    Map<Id, FeedItem> feedItemMap = new Map<Id, FeedItem>([SELECT Id, Body FROM FeedItem WHERE Id IN :feedItemIds]);

    for(FeedComment f : trigger.new)
    {
        if(f.CommentBody.startsWith('!newCase'))
        {
            Case newCase = new Case(Subject = f.CommentBody,
            						Description = feedItemMap.get(f.FeedItemId).Body,
					                Origin = 'Chatter',
					                RecordtypeId = '012G0000000yArc',
					                Status = 'Open');
            caseList.add(newCase);
        }
    }

    insert caseList;
}

 try this.

This was selected as the best answer
akallioWileyakallioWiley

That works. Kind of a bummer that the soql query is necessary...but then again I had dml inside the for loop!

 

Thanks!