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.