2

Is there any way other than Apex Trigger/Process Builder/Workflow to capture record events programmatically? Does Salesforce have any sort of CRUD event publisher that can be somehow subscribed to?

My requirement is simple: do X when object Y is inserted/updated/deleted. However both X & Y are dynamic, and it is not possible to create a new trigger every time Y changes.

The problem is, my client has an existing, fragile solution in place that is tightly integrated with triggers and workflows. But my solution needs to detect CRUD events and act upon them in its own thread.

7
  • 1
    You can use triggers (OK you have to create one per object type) to call whatever Apex handling framework you want to build (using the base SObject type) and that framework can be controlled by configuration such as JSON. But I think you need to explain what you mean by "dynamic" and also explain what logic are you looking to build to get a good answer here.
    – Keith C
    Mar 5, 2016 at 23:27
  • Did you think about leveraging the metadata API somehow for this? Mar 6, 2016 at 1:18
  • @SebastianKessel do you mean to create triggers via the API? As per my requirement it is not possible for me to create new triggers.
    – Mossi
    Mar 6, 2016 at 2:07
  • I was not necessarily saying that. Why are triggers not available? You have not explained why. Mar 6, 2016 at 2:09
  • I appreciate trying to break down the problem, but let's stick to the question. I cannot create new triggers because my client doesn't want them. They have an existing, fragile solution in place that is tightly integrated with triggers and workflows. But my solution needs to detect CRUD events and act upon them in its own thread. Of course this is my ideal route. If there is no solution here then I have to look into a suboptimal plan B (which I already know about).
    – Mossi
    Mar 6, 2016 at 2:16

1 Answer 1

2

There are a couple options:

The above should give you insight into Create, Update, and Delete. Tracking Read access is trickier. As long as your API Version is 28+, you can query for LastViewedDate in your feed. I am not sure how that will integrate with your Syndication Feeds if you take the Site approach.

You can probably set this all up on one Visualforce Page with something like the following:

public PushTopic topic { get; private set; }
public FeedController()
{
    String sObjectType = ApexPages.currentPage().getParameters().get('sObjectType');
    String operation = ApexPages.currentPage().getParameters().get('operation');
    String topicName = sObjectType + '_' + operation;
    if (Schema.getGlobalDescribe().get(sObjectType) == null) return;

    List<PushTopic> topics = [SELECT Id FROM PushTopic WHERE Name = :topicName];
    if (!topics.isEmpty()) { topic = topics[0]; }
    else
    {
        topic = new PushTopic(Name=topicName, ApiVersion=36);
        String baseQuery = 'SELECT Id FROM ' + sObjectType;
        if (operation == 'create')
            topic.Query = baseQuery + ' WHERE CreatedDate = TODAY';
        else if (operation == 'update')
            topic.query = baseQuery + ' WHERE LastModifiedDate = TODAY';
        else if (operation == 'delete')
            topic.query = baseQuery + ' WHERE IsDeleted = true ALL ROWS';
        insert topic;
    }
}
8
  • This solution has to work with or without a Site, without user interaction. But I can see where you are going with this. Basically subscribe to all records, and have the CUD events streamlined through Feeds -right? But doesn't this require creating a Feed for every record in the system? So if the client chooses to perform an Apex-defined action on update of any Account or Contact the corresponding Feeds must already exist. Is that right?
    – Mossi
    Mar 6, 2016 at 3:54
  • do Chatter Feeds help here?
    – cropredy
    Mar 6, 2016 at 6:26
  • @crop1645 you can track updates that way, definitely. Similar idea.
    – Adrian Larson
    Mar 6, 2016 at 14:27
  • @Mossi Were you able to pursue the Streaming API at all? It seems like it should work, but I haven't tried it in this level of depth.
    – Adrian Larson
    Mar 10, 2016 at 20:02
  • @AdrianLarson I know I haven't provided much insight about my business needs, but there are several issues with using the Streaming API for me: 1) there is a limit of 40-50 PushTopics and 1000 subscribers per org, 2) PushTopics don't return prior values like Trigger.old does, 3) staying tuned to PushTopics requires a VF page (I may be able to use a sidebar VF component but even then it won't work in LEX). I also remember reading somewhere that each PushTopic is valid for 24 hours only? I may be wrong.
    – Mossi
    Mar 10, 2016 at 21:36

This site is temporarily in read-only mode and not accepting new answers.

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