Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. trigger LeadDeduplicate on Lead (after insert) {
  2.  
  3. List<Account> accounts = [SELECT ID, Account_ID__c, Siret__c, OwnerId FROM Account WHERE Account_ID__c != NULL AND CreatedDate > :System.Today() - 30];
  4. List<Contact> contacts = [SELECT ID, Email, MobilePhone, AccountId, OwnerId FROM Contact WHERE CreatedDate > :System.Today() - 30];
  5. Boolean leadIsConverted = false;
  6.  
  7. for(Lead lead : trigger.new) {
  8. // Trigger only if the lead comes from external
  9. if(lead.IsExternal__c) {
  10. for(Account account : accounts) {
  11. if(!leadIsConverted) {
  12. if(Utils.hasTheSameAccountId(lead, account) || Utils.hasTheSameSiret(lead, account)) {
  13. Utils.mergeLeadWithAccount(lead, account);
  14. leadIsConverted = true;
  15. }
  16. }
  17. }
  18.  
  19. if(!leadIsConverted && !contacts.isEmpty()) {
  20. for(Contact contact : contacts) {
  21. if(!leadIsConverted) {
  22. if(Utils.hasTheSameEmail(lead, contact) || Utils.hasTheSameMobile(lead, contact)) {
  23. Utils.mergeLeadWithContact(lead, contact);
  24. leadIsConverted = true;
  25. }
  26. }
  27. }
  28. }
  29. }
  30. leadIsConverted = false;
  31. }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement