2

I have a system set up that creates a Chatter post whenever a Contact is created. This works great when the record is created via the UI. We recently integrated with an external system that creates Contacts using the Webservices Site Guest User. Whenever a Contact is attempted to be created in this way, a Fatal error occurs and prevents the record from being created.

Here is the error I get:

FATAL_ERROR|ConnectApi.ConnectApiException: Guest users are not permitted in internal communities

Here's the code that creates the Chatter Post:

User u = [SELECT Id FROM User WHERE FirstName = 'John' and LastName = 'Doe'];

        Id uId;

            if(u.id != null){
                uId = u.id;
            }
            else{
                uId = UserInfo.getUserId();
                }
ChatterUtils.mentionLinkPost(uId ,userList[0].id,URL.getSalesforceBaseUrl().toExternalForm()+'/apex/Confirmation?contId='+contItem.id,'Accept Lead for '+contItem.FirstName+' '+contItem.LastName);

Here's the Chatter Utils Code:

    public static void mentionLinkPost(Id userId, Id userToMentionId, String url, String urlName) { 

    ConnectApi.FeedItemInput feedInput = new ConnectApi.FeedItemInput();
    feedInput.body = new ConnectApi.MessageBodyInput();

    // add some text before the mention
    ConnectApi.TextSegmentInput textSegment = new ConnectApi.TextSegmentInput();
    feedInput.body.messageSegments = new List<ConnectApi.MessageSegmentInput>();  
    textSegment.text = 'You have a new Lead ';
    feedInput.body.messageSegments.add(textSegment);

    // add the mention
    ConnectApi.MentionSegmentInput mentionSegment = new ConnectApi.MentionSegmentInput();
    mentionSegment.id = userToMentionId;
    feedInput.body.messageSegments.add(mentionSegment);

    /*// add the text that was passed
    textSegment = new ConnectApi.TextSegmentInput();
    textSegment.text = postText;
    messageInput.messageSegments.add(textSegment);*/

    ConnectApi.LinkAttachmentInput linkIn = new ConnectApi.LinkAttachmentInput();
    linkIn.urlName = urlName;
    linkIn.url = url;
    feedInput.attachment = linkIn;

    /*ConnectApi.FeedItemInput input = new ConnectApi.FeedItemInput();
    input.body = feedInput;*/

    // post it
    ConnectApi.ChatterFeeds.postFeedItem(null, ConnectApi.FeedType.UserProfile, userId, feedinput, null);

  }

This results in a Chatter post that Mentions as follows "UserWhoCreatedContact to John Doe" then the post body follows instead of the mention coming from "John Doe" followed by the post body. I want to set the user who creates the post to John Doe but have been unsuccessful so far.

Any thoughts as to how I could accomplish this? Any help is much appreciated.

2

There are a few different things going on:

  1. The Connect API doesn't support impersonation (posting a feed item as a different user). It's not possible to call ConnectApi.ChatterFeeds.postFeedItem() and have the feed item posted as John Doe unless John Doe is the current, logged-in user. In your code, you're using the userId as the third parameter, which is the subjectId -- that's the parent of the feed item, not the author of the feed item.

  2. The guest user is read-only and can't call ConnectApi.ChatterFeeds.postFeedItem().

  3. It's kind of a moot point due to point 2, but the guest user is associated with a community and can't access the internal network. The first parameter to ConnectApi.ChatterFeeds.postFeedItem() is the communityId, and when you pass in null, that means the internal network. That's why you're getting the "Guest users are not permitted in internal communities" error. But even if you used the community ID, you'd still be unable to post due to point 2.

  • Thanks @alouie. It sounds like I'm somewhat screwed here? – sfdcJameson Aug 19 '15 at 15:12
  • If you need impersonation, the only way to do it is to use the FeedItem sObject. Search for "Insert System Field Values for Chatter Feeds" on developer.salesforce.com/docs/atlas.en-us.api.meta/api/…. I still don't think the guest user will be able to do the insertion, though. Also, @mentions aren't supported when inserting via sObject. – alouie Aug 19 '15 at 19:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.