×
Search icon Contact Us

Blog

Students taking an in-person training class from Stony Point

How to Share a Salesforce Record from a Chatter Post

As you probably know, Stony Point is a leading provider of Salesforce Training. I frequently teach Salesforce Developer Classes, Salesforce Administrator Classes and Salesforce Consulting Classes. Last week I was teaching a private, Salesforce Administration in Lightning (SP-ADX201) to a group of developers in Cypress, CA. Normally, when I do Salesforce developer training, the developers ask a lot of challenging questions. This group was no different.

The Challenge

They were implementing Salesforce and would have millions of cases each year. The case assignment rules for adding Case Teams were going to be complex, to say the least. They wanted a private sharing model, and understood that users without access to the Case wouldn’t be able to see Chatter posts on the Case. After discussing options for sharing case records, they asked this question:

“Is it possible to share a Salesforce Case record based on an @mention in a Salesforce Chatter Post?”

This was an intriguing question that I hadn’t heard before. The use case was clever. If for some reason, a Salesforce Case record wasn’t shared properly with a Salesforce User, couldn’t we make it easy for someone with access to share it with another user? The easiest way to do this would be to @mention the Salesforce User and then have the system automatically share the record with the User who was mentioned.

I knew it was possible to write a Trigger on the Salesforce Chatter Feed object, but I didn’t know if it was possible to get the record ID of the Salesforce User who had been @mentioned. After some research, I discovered it was possible. In order to do it, you have to use the ConnectApi to get the details of the Chatter Feed, but it is possible. The answer is definitely aimed toward a Salesforce developer and would be an appropriate challenge exercise for our Programmatic Development using Apex and Visualforce in Lightning Experience (SP-DEX450) Salesforce Developer course.

The Solution

Below is the code I wrote share a Case record with a Salesforce User who is @mentioned on the Salesforce Chatter Feed for the Salesforce Case.

trigger ShareChatterOnFeed on FeedItem (after insert) {
	String communityId = Network.getNetworkId();
    List<String> feedItemIds = new List<String>();
    List<CaseShare> shares = new List<CaseShare>();

    for (FeedItem f : trigger.new) {
        feedItemIds.add(f.id);
    }

    //@mentions are only available in the API, so call the API
    //get the chatter entries
    ConnectApi.BatchResult[] results = ConnectApi.ChatterFeeds.getFeedElementBatch(communityId, feedItemIds);
    //loop through the results
	for (ConnectApi.BatchResult result : results) {
        if (result.isSuccess()) {
            //create a case share object
            CaseShare cs = new CaseShare();
            Object theResult = result.getResult(); 
            ConnectApi.FeedElement e = (ConnectApi.FeedElement) theResult;
            Id parentId = e.parent.Id;                        
            String sobjectType = parentId.getSObjectType().getDescribe().getName();
            if(sObjectType == 'Case') {                                      
                cs.CaseId = parentId;
                cs.CaseAccessLevel = 'read';
                cs.RowCause = Schema.CaseShare.RowCause.Manual;                
            }
            //system.debug(sObjectType);
            if (theResult instanceof ConnectApi.FeedItem) {
                ConnectApi.FeedItem item = (ConnectApi.FeedItem) theResult;                
                for (ConnectApi.MessageSegment segment : item.body.messageSegments) {
                    if (segment instanceof ConnectApi.MentionSegment) {
                        ConnectApi.MentionSegment theMention = (ConnectApi.MentionSegment) segment;
                        Id mentionedId = theMention.record.id;                        
                        //System.debug('Mentioned ID: ' + mentionedId);  
                        cs.UserOrGroupId = mentionedId;
                		shares.add(cs);
                    }
                }
            }
        }
    }

    if (shares.size() > 0) {
        List<Database.saveResult> results = Database.insert(shares,false);
        for (Database.saveResult sr : results) {
            //system.debug(sr);   
        }
    }
    
}

I hope you found this Salesforce Apex code useful.

Salesforce Developer Training

If you or anyone you know is in need of Salesforce training, please give us a call! We offer Salesforce Developer training, Salesforce Administrator training, or custom Salesforce training. We are a leader in Salesforce training and can provide Salesforce training around the world in many different languages.

 
Yellow Stony Point icon

Want to Talk with Our Team?

Whatever your cloud needs are, Stony Point can help. We have worked with hundreds of companies and launched successful training, consulting and staffing projects all around the globe. Submit this form, or call us to chat live: +1 (844) 978-6697