2

I'm using the Twilio API to send SMS text messages to my app via HTTP POST coming into a REST site on my org. When the message comes in, a Message__c record is created and attached to a parent Conversation__c record with an OwnerId (a user). I need the user to get a push notification on Salesforce1 that a new message has arrived.

With Salesforce1, the only apparent way to trigger the notification is to post to chatter (either to a user's profile, or with a @mention). I finally figured out to set the Public Access Settings on the site to enable Chatter. So now, I can get a Chatter post to happen... but it doesn't trigger the notification. Not sure why?

I spent lots of time researching the PushNotification class, but that's for custom mobile apps -- doesn't work with Salesforce1.

I found that I had to use APEX/trigger rather than Process Builder to post to chatter -- Process Builder won't handle the chatter post from the Site Guest User, even if the Chatter is enabled for the guest user profile.

I've tried APEX for adding a feedItem -- very easy, and works, but no notification is triggered. Also tried ConnectApi:

ConnectApi.FeedElement feedElement = 
            ConnectApi.ChatterFeeds.postFeedElement(Network.getNetworkId(), ParentId, ConnectApi.FeedElementType.FeedItem, Body);

Also works, but no notification. I tried setting the CreatedBy for the feedItem, but that's a System Field setting that the Guest User can't be assigned.

The notifications DO work if I trigger these methods from Exec Anon, because in those cases, the context user is me (with Sys Admin profile). So the code is "right", but the Site Guest User just can't quite do it.

Any ideas, hacks, workarounds?

I did see this old blog post for a workaround hack to let a person post to Chatter with an @mention to herself, that uses email to case to generate a Case, then sets Automated Case User to System, then uses that System user to post to Chatter, then a process builder to delete the case that was created. It's not really what I'm trying to accomplish, but the hack manages to engage a System User context. Could that work for my situation? This is for a managed package app, so I'm leary of creating Cases then deleting them.

0

It would seem that Platform Events run as "Automated Process". What if you publish a Platform Event that launches a trigger or Process Builder that makes the Chatter post?

  • good idea. I was just wondering about that (in the middle of the night, when I tend to lie awake and ponder things!) I'll try and let you know. – PatMcClellan__c Jul 14 '18 at 16:46
0

OK, thanks to Charles T's idea, I got it working.

I ditched the trigger on the new message record creation. Instead, replaced it with a Platform Event, including the Id for the Conversation that got a new message.

I tried doing a trigger on the platform event, but it just wouldn't fire -- not sure why. So instead, I created a Lightning Flow with a Wait element subscribed to the Platform Event.

The flow grabs the event and does a record lookup on the Conversation matching the event's field, ConversationId. Then, I simply post to Chatter, directing it to the User matching the Conversation's OwnerId, and a related chatter message.

The Chatter post appears to be "from" the Admin who runs the Flow.

enter image description here

Woo-hoo! That's been vexing me for days.

  • Neat, glad it is working! You might have also been able to do this in Process Builder. When you create a PB from a Platform Event it asks you to choose a field on the PE that it uses to look up a matching SObject record. – Charles T Jul 15 '18 at 18:34
  • Cool. I used flow because I didn't think I could do a record lookup. Good to know! – PatMcClellan__c Jul 16 '18 at 19:25
  • Yeah I was surprised too. Only PEs seem to support such a lookup. You can match on external ID too I think. – Charles T Jul 16 '18 at 19:33
  • @CharlesT -- I tried it with Process Builder, and the original problem emerged. I get the error: Guest users are not permitted to perform this operation. So Process Builder doesn't run in System context, where Flow runs in the context of the user who initiates the Run on the flow. – PatMcClellan__c Jul 16 '18 at 23:22

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.