Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- trigger LeadDeduplicate on Lead (after insert) {
- List<Account> accounts = [SELECT ID, Account_ID__c, Siret__c, OwnerId FROM Account WHERE Account_ID__c != NULL AND CreatedDate > :System.Today() - 30];
- List<Contact> contacts = [SELECT ID, Email, MobilePhone, AccountId, OwnerId FROM Contact WHERE CreatedDate > :System.Today() - 30];
- Boolean leadIsConverted = false;
- for(Lead lead : trigger.new) {
- // Trigger only if the lead comes from external
- if(lead.IsExternal__c) {
- for(Account account : accounts) {
- if(!leadIsConverted) {
- if(Utils.hasTheSameAccountId(lead, account) || Utils.hasTheSameSiret(lead, account)) {
- Utils.mergeLeadWithAccount(lead, account);
- leadIsConverted = true;
- }
- }
- }
- if(!leadIsConverted && !contacts.isEmpty()) {
- for(Contact contact : contacts) {
- if(!leadIsConverted) {
- if(Utils.hasTheSameEmail(lead, contact) || Utils.hasTheSameMobile(lead, contact)) {
- Utils.mergeLeadWithContact(lead, contact);
- leadIsConverted = true;
- }
- }
- }
- }
- }
- leadIsConverted = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement