0

I'm authorizing users through oAuth (Connected app) via a PHP app. I then want to enable an Apex trigger on every user's account; in short, I want to send all contacts to my app as they're added. I have the Apex trigger written and working, but I'm lost as how to enable the trigger on a per-user basis.

Thanks for any guidance you can provide.

0

2 Answers 2

5

Expanding my comment into an answer.

I'd suggest you considered using the new Change Data Capture feature instead. Synchronization of Salesforce data to an external persistent store is exactly what it's made for; it allows your application to listen for change events and provides for replay and recovery should you miss portions of the event stream (within a time window). CDC guarantees ordered delivery to your application, as well.

If you don't want to use CDC, you can also do an initial bulk load and then poll with sObject Get Updated, or its SOAP equivalent.

Data sync triggers are really hard to get right 100% of the time. You have to deal with callout limits, asynchronous Apex code, managing ordered delivery guarantees yourself (if you need to), handling error recovery and redelivery, and so forth. Lots of orgs roll quick and dirty sync triggers with @future callouts; I really recommend not going down this path - as your enterprise systems grow in complexity it rapidly becomes unmaintainable and unreliable.

To directly answer your question: trigger by nature are not per-user; they run in system context on all relevant trigger events. You need to implement per-use logic if and only if you wish to selectively deactivate your trigger on a per-user basis. There are a variety of patterns for doing so; I think the Custom Permission route is the best of them.

1
  • Thanks for the pointers. I ended up going the sObject get updated route since the version we're using doesn't support CDC.
    – jeffkolez
    Commented Dec 17, 2019 at 17:45
0

@Jeffkolez , As you said you already created an API connection from your PHP app to Salesforce. Same way you can create an API in your PHP app, which can be called from Salesforce.

Once your PHP API is ready, you can call that from your Apex Trigger as and when contacts are being updated or added, deleted etc. Any time some changes occurs to the contact your PHP API will be called. Else you are restrict the trigger to work only on after Insert operation.

For per user basis you can use Permission sets, which can be a criteria in your trigger to check if any user has that permission set or not and then fire for them if they have the permission set assigned. Hope this helps!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .