Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the user-registration
domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init
action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /customers/c/c/d/ccloudonline.nl/httpd.www/wp-includes/functions.php on line 6121
Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the hashone
domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init
action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /customers/c/c/d/ccloudonline.nl/httpd.www/wp-includes/functions.php on line 6121
Warning: Cannot modify header information - headers already sent by (output started at /customers/c/c/d/ccloudonline.nl/httpd.www/wp-includes/functions.php:6121) in /customers/c/c/d/ccloudonline.nl/httpd.www/wp-includes/rest-api/class-wp-rest-server.php on line 1896
Warning: Cannot modify header information - headers already sent by (output started at /customers/c/c/d/ccloudonline.nl/httpd.www/wp-includes/functions.php:6121) in /customers/c/c/d/ccloudonline.nl/httpd.www/wp-includes/rest-api/class-wp-rest-server.php on line 1896
Warning: Cannot modify header information - headers already sent by (output started at /customers/c/c/d/ccloudonline.nl/httpd.www/wp-includes/functions.php:6121) in /customers/c/c/d/ccloudonline.nl/httpd.www/wp-includes/rest-api/class-wp-rest-server.php on line 1896
Warning: Cannot modify header information - headers already sent by (output started at /customers/c/c/d/ccloudonline.nl/httpd.www/wp-includes/functions.php:6121) in /customers/c/c/d/ccloudonline.nl/httpd.www/wp-includes/rest-api/class-wp-rest-server.php on line 1896
Warning: Cannot modify header information - headers already sent by (output started at /customers/c/c/d/ccloudonline.nl/httpd.www/wp-includes/functions.php:6121) in /customers/c/c/d/ccloudonline.nl/httpd.www/wp-includes/rest-api/class-wp-rest-server.php on line 1896
Warning: Cannot modify header information - headers already sent by (output started at /customers/c/c/d/ccloudonline.nl/httpd.www/wp-includes/functions.php:6121) in /customers/c/c/d/ccloudonline.nl/httpd.www/wp-includes/rest-api/class-wp-rest-server.php on line 1896
Warning: Cannot modify header information - headers already sent by (output started at /customers/c/c/d/ccloudonline.nl/httpd.www/wp-includes/functions.php:6121) in /customers/c/c/d/ccloudonline.nl/httpd.www/wp-includes/rest-api/class-wp-rest-server.php on line 1896
Warning: Cannot modify header information - headers already sent by (output started at /customers/c/c/d/ccloudonline.nl/httpd.www/wp-includes/functions.php:6121) in /customers/c/c/d/ccloudonline.nl/httpd.www/wp-includes/rest-api/class-wp-rest-server.php on line 1896
{"id":878,"date":"2017-04-28T14:41:43","date_gmt":"2017-04-28T14:41:43","guid":{"rendered":"http:\/\/ccloudonline.nl\/?p=878"},"modified":"2023-01-17T23:11:20","modified_gmt":"2023-01-17T23:11:20","slug":"triggers-helping-to-catch-duplicates","status":"publish","type":"post","link":"http:\/\/ccloudonline.nl\/?p=878","title":{"rendered":"Triggers – Helping to catch duplicates"},"content":{"rendered":"
One way in which triggers can be of great help is by using them to catch duplicates records before insertion or before doing an update. Probably the easiest way to do this is by making sure you have implemented a unique key identifier on those records that you want to control and make sure that will not contain any duplicates.<\/p>\n
The unique identifier needs to be, well… ‘unique’, and no other record should contain that same key.
\nDepending on the object in which you want to implement your “duplication preventer” you need to come up with that field that will contain your unique identifier.<\/p>\n
Let’s take the Lead object for our test here, and I am going to use two unique values fields. Normally you should be able to use something like the Email Address field, but we are going to use the Phone<\/strong> and Mobile Phone<\/strong> for this example.<\/p>\n The business requirements for this are the following:<\/p>\n 1.<\/strong> We need to stop data contamination on new Leads coming to our SFDC org. It is clear what we want to accomplish here, let’s take a look at the code.<\/p>\n This code will check for Phone<\/strong> and MobilePhone<\/strong> numbers on any Lead before you try to insert or update a record. If it finds that any of those two fields are equal to a Lead record already in the system it will fire an error on the field that is causing the possible duplication.<\/p>\n \/******************************************************************************** trigger leadPhoneAndMobilePhoneDup on Lead (before insert, before update){<\/p>\n \/\/We created a Map to insert or Leads for (Lead lead : System.Trigger.new) \/\/We want to target those new leads with a Phone and compare them with the phones \/\/Same operation but targeting the MobilePhone if (leadMap.containsKey(lead.MobilePhone)) \/\/We query our Leads and use the Phone as the reference. If found on our map, throw an error \/\/We query our Leads and use the Mobile Phone as the reference. If found on our map, throw an error Some interesting points here are the oldMap<\/strong> and how it will help us to create a key using the fields in our page layout.<\/p>\n It is important to remember that oldMap: Is a map of IDs to the old versions of the sObject records. You can better get all this logic in a helper class and just use the trigger to invoke that class and execute the logic.<\/p>\n","protected":false},"excerpt":{"rendered":" Triggers – Helping to catch duplicates. One way in which triggers can be of great help is by using them to catch duplicates records before insertion or before doing an update. Probably the easiest way to do this is by making sure you have implemented a unique key identifier on those records that you want […]<\/p>\n","protected":false},"author":1,"featured_media":640,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-878","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-triggers"],"_links":{"self":[{"href":"http:\/\/ccloudonline.nl\/index.php?rest_route=\/wp\/v2\/posts\/878","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/ccloudonline.nl\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ccloudonline.nl\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/ccloudonline.nl\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/ccloudonline.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=878"}],"version-history":[{"count":15,"href":"http:\/\/ccloudonline.nl\/index.php?rest_route=\/wp\/v2\/posts\/878\/revisions"}],"predecessor-version":[{"id":5946,"href":"http:\/\/ccloudonline.nl\/index.php?rest_route=\/wp\/v2\/posts\/878\/revisions\/5946"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/ccloudonline.nl\/index.php?rest_route=\/wp\/v2\/media\/640"}],"wp:attachment":[{"href":"http:\/\/ccloudonline.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=878"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ccloudonline.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=878"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ccloudonline.nl\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=878"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}
\n2.<\/strong> Phone and Mobile Phone should be the identifiers
\n3.<\/strong> If any of those values are in the system assigned to an existing Lead, an error should be displayed to the user
\non the field causing the alert.
\n4.<\/strong> Saving of the new record shouldn’t be allowed<\/p>\n
\n*
\n* Trigger on Leads to catch duplicates based on Phone and Mobile Phone
\n*
\n********************************************************************************\/<\/p>\n
\nMap<String, Lead> leadMap =new Map<String, Lead>();<\/p>\n
\n{<\/p>\n
\n\/\/of leads that are already in our database.
\nif ((lead.Phone !=null) &&
\n(System.Trigger.isInsert ||
\n(lead.Phone != System.Trigger.oldMap.get(lead.Id).Phone)))
\n{
\n\/\/Firing error if we find a lead that already has the same Phone
\nif (leadMap.containsKey(lead.Phone))
\n{
\nlead.Phone.addError(‘Another new lead has the same Phone address.’);
\n\/\/If not found, we add the lead to our leadMap
\n}else{
\nleadMap.put(lead.Phone, lead);
\n}
\n}<\/p>\n
\nif ((lead.MobilePhone !=null) &&
\n(System.Trigger.isInsert ||
\n(lead.MobilePhone != System.Trigger.oldMap.get(lead.Id).MobilePhone)))
\n{<\/p>\n
\n{
\nlead.MobilePhone.addError(‘Another new lead has the same Phone address.’);
\n\/\/If not found, we add the lead to our leadMap
\n}else{
\nleadMap.put(lead.MobilePhone, lead);
\n}
\n}
\n}<\/p>\n
\nfor (Lead lead: [SELECT Phone FROM Lead
\nWHERE Phone IN :leadMap.KeySet()])
\n{
\nLead newLead = leadMap.get(lead.Phone);
\n\/\/Error needs to be on the field that is causing it, in this case, the Phone field
\nnewLead.Phone.addError(‘A lead with this Phone address already exists.’);
\n}<\/p>\n
\nfor (Lead lead: [SELECT MobilePhone FROM Lead
\nWHERE MobilePhone IN :leadMap.KeySet()])
\n{
\nLead newLead = leadMap.get(lead.MobilePhone);
\n\/\/Error needs to be on the field that is causing it, in this case, the Mobile Phone field
\nnewLead.MobilePhone.addError(‘A lead with this Mobile Phone address already exists.’);
\n}
\n}<\/p>\n
\nThis map is only available in “update” and “delete” triggers.<\/em><\/p>\n