function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
mhson1978mhson1978 

Trigger to catch duplicate record and process the new data

Hello Everyone. I am very new to SalesForce development and would like to ask some questions regarding Triggers.

 

My company has a need to capture duplicate leads and process them by merging information.

 

After reading through documentation, I thought trigger would be the best way to do this.

 

To test the process, i created a custom object and wrote "before insert" trigger against it.

This trigger essentially searches for record based on email address and if record exists, it updates the visit count. However, I failed on this matter since after it processes the new record, it STILL inserts the duplicate record.

 

I've tried to use both before insert, after insert events.

My thought was, before insert, merge the record and after insert, delete the newly created record.

This also failed since I can NOT perform DML against trigger.new records.

 

Has anyone faced this type of issue before and if so, how did you resolve this?

 

Any assistance would be greatly appriciated.

 

thank you. 

jeffdonthemicjeffdonthemic

I'm not exactly sure if this fits your business case, but open the Apex docs (link) and search for "addError". Using addError() marks a record with a custom error message and prevents any DML operation from occurring.

 

You might also want to look at the difference between DML statements and DML database method for exception handling.

 

Good luck

 

Vote for my Idea: Enhanced Apex Testing Functionality (JUnit) Link

 

 

Jeff Douglas
Informa Plc
http://blog.jeffdouglas.com

 

 

dev_jhdev_jh

Hi there,

 

It might be worth taking a look for existing tools in the Apexchange for merging leads. I am not sure a trigger is the best way to achieve this...

 

If you still want to go ahead with the trigger, there is one option. You have your trigger do the processing as it does and then throw up an error (by using addError). This way the record will not be saved and instead the user will get some sort of message. The user will then just click cancel I guess. Just an idea of how to achieve this using triggers. Not very elegant.

 

Regards,

 

J

mhson1978mhson1978

Hello all. Thank you for your response.

I've actually thought about using existing merge tools and/or adding addError.

 

Our leads are generated from our company website. When ever someone downloads an article, our website makes a service call out to SalesForce to create the new Lead.

 

This is why I am going to the trigger method to handle any duplicate leads being created since new leads are generated off of the website.

 

In terms of adding addError method, it's not really merging the data.

 

So, a sample process would be this:

before insert ->

1. Check to see if lead exists in the Leads where emailAddress = Trigger.new emailAddress.

1a. If it exists, update the document list, counts, last modified date, user info.. etc.

1a1. Once updated, throw error so it will not insert the new record.

 

  1b. If Not exists, add new lead.

 

Based on above case, SalesForce does in fact throw an error but it doesn't update the existing record.

I'm assuming this is becasue everything is done in batch transaction so if one fails, everything else fails.

 

Has anyone implemented auto merging capabilities with web generated leads?

 

thank you. 

dev_jhdev_jh

Hi there,

 

AddError should work on a record by record basis. I seem to remember (although I haven´t used this yet) that you can use transactions... maybe this could help towards making the update but not commiting the insert?. Also, make sure the AddError is used within a try / catch statement so it is handled...

 

Anyone has additional ideas? Let us know if you manage to get this working and how. All the best,

 

J

mhson1978mhson1978

Actually, I've been thinking this through and I think we NEED to have these duplicate leads created.

Even If i merge certain types of information, I think I still want users to be involved when removing duplicate records.

 

I'm thinking I'll use or add a flag on the Leads record to indicate which is active one.

When the trigger find the new entry as duplicate, it will merge the data and set the flag as false.

 

I'm still juggling with this idea at the moment.

 

To go back to your answer, how do you do transactions? 

jeffdonthemicjeffdonthemic

Take a look at Transaction Control in the Apex docs: Link

 

 

Vote for my Idea: Enhanced Apex Testing Functionality (JUnit) Link

 

 

Jeff Douglas
Informa Plc
http://blog.jeffdouglas.com

farhanafarhana

hi there,

 I am trying to do exactky same thing. Like posted, upsert, create new record and also updates the existing. Did you come accross any solution? I need help. 

 

Please and thank you.

Farhana