1

I have this problem with triggers:

We have integration with our backoffice to receive leads. Unfortunately, we can't change this integration. It sends the leads when when it shouldn't.

On insert if I find an existing duplicate lead I don't want to insert it. I do lead.addError('This lead already exists in the system') and it works fine.

The problem is that I also need to create a task for this existing lead owner saying something along the lines of

user submitted the form again

This task is an important signal for the sellers and they want to be notified when that happens. The problem is that if I do addError() in the trigger it rolls everything back and doesn't insert my task.

How can I prevent the lead from being inserted while creating a task at the same time?

Or is there a better way to approach this?

  • Would be easier for us if we could see the code and what it is that you have done so far. If you are working with a trigger and you are rolling back I think is necessary for us to see that code before giving you any direction that may help you. I can't see why you can't send Tasks if a record fails. There are ways to control and build logic around falling records. – Carlos Naranjo Jan 30 at 15:33
  • Platform event can still be published event if entire transaction rolled back. Based on that you can log a task in separate execution context – kurunve Jan 30 at 16:29
  • @kurunve, how would you set up a separate context in a trigger ? – Anton Solomin Jan 30 at 16:42
  • 1
    But firing a platform event (with Publish Immediately option - help.salesforce.com/…) and creating task then in event trigger or maybe process builder – kurunve Jan 31 at 7:44
  • You can also put a Savepoint after creating the Task so it doesn't matter if you rollback after that, the task creation will be preserved. – Alexander Aeons Torn Feb 6 at 12:15
0

As Kurunve suggested it is indeed possible to do this with a platform event with "publish immediately" option and a process builder.

  1. Create a platform event and some custom fields that you'll need to create a task or whatever object you need.

  2. In the lead trigger before you do .addError() publish your platform event. It can look something along these lines:

    Database.SaveResult sr = EventBus.publish(new ABTask__e(
        AccountOwnerID__c = ownerId,
        Account_ID__c = accountId
    ));
    if (sr.isSuccess()) {
        System.debug('Successfully published event.');
    } else {
        for (Database.Error err : sr.getErrors()) {
            System.debug('Error returned: ' + err.getStatusCode() + ' - ' + err.getMessage());
        }
    }
    
  3. Subscribe to your event. You can do it in a trigger on after insert or with a Process Builder.

  4. Voilà, you can insert/update records independent of the trigger context.

| improve this answer | |

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.