-1

The following is some code that was given to me to try but it has issues that I am having trouble with. With the code, I am trying to mark a checkbox = True, if the email address of a contact (any contact) on the account or within the system (global) is unique,. ie. has no duplicate email. I am currently getting a Error: Compile Error: static can only be used on methods of a top level type at line 8 column 18 Many thanks for the help!

trigger UpdateCheckbox on Contact (before insert) {
    if(trigger.isBefore && trigger.isInsert){
        ContactUpdate.ContactUpdate(trigger.new);

    }

public class ContactUpdate {
     Public static void ContactUpdate(List<Contact> conList) {
        try{
            if(conList.size() > 0){

                List<Contact> conUpdateList = new List<Contact>();                
                List<Contact> conCheckList = [SELECT Id,Name,Email FROM Contact LIMIT 10000];

                for(Contact con: conList) {
                    if(con.Email != null) {
                        for(Contact conCheck: conCheckList){
                            if(con.Email != conCheck.Email){
                                con.Checkbox__C == TRUE;
                                conUpdateList.add(con);
                            }
                        }
                    }
                }
                if(conUpdateList.size() > 0 ){
                    update conUpdateList;
                }
            }
        }catch (Exception e){
            system.debug('Exception:'+e.getMessage()+' '+e.getLineNumber());
        }
        }

    }
}
  • Hi JSlateiv, welcome to SFSE! Please take a moment to scroll through the tour and read How to Ask, then edit your post to include the error message verbatim, and clearly indicate which line the stack trace points to. – Adrian Larson Jul 16 at 15:42
  • Did you try to put Apex Class code below Trigger COde? – Pranay Jaiswal Jul 16 at 15:45
  • Thanks for the newbie help Adrian, I will update – JSlateiv Jul 16 at 16:06
  • Yes, tried to close out trigger code completely and begin new with Class, but get Error: Compile Error: Missing '<EOF>' at 'public' at line 7 column 1 – JSlateiv Jul 16 at 16:12
1

This is not good code to deploy to your org, and I would strongly recommend you not attempt to do so. It will not work and will likely begin causing problems immediately that will block you from creating Contacts.

List<Contact> conCheckList = [SELECT Id,Name,Email FROM Contact LIMIT 10000];

for(Contact con: conList) {
    if(con.Email != null) {
        for(Contact conCheck: conCheckList){
            if(con.Email != conCheck.Email){
                con.Checkbox__C == TRUE;
                conUpdateList.add(con);
            }
        }
    }
}

This is

  • A highly inefficient O(N*M) search, which may cause LimitExceptions due to CPU time limits. Ask yourself: how many comparisons will this make if I update 100 Contacts in a data upload? Because every Contact will be compared against some set of 10,000 Contacts pulled from your org, that will require 1,000,000 comparison operations.
  • Implemented incorrectly. This code

    • Makes no attempt to query only those Contacts which share Email values with those currently in the trigger.
    • Does not do what you want it to do. You want to set the field Checkbox__c to true if the Email address is unique. Instead, this sets the checkbox to true if any of the email addresses of these 10,000 Contacts you've queried at random differ from the current record.
    • Will cause DmlExceptions because it adds the same Contact to conUpdateList multiple times.
    • Will not clear the checkbox for any Contacts that are no longer unique based on changes.

This code needs to be rewritten from scratch.

Instead, I would encourage you to evaluate whether the native, declarative Duplicate Management feature can meet your underlying objectives and needs.

  • Thanks David and that makes sense when explained as you do. I unfortunately don't have much experience with Apex but am trying to learn. The duplicate management feature is of no value to what I am trying to accomplish, which is specifically mark unique contacts that do not have an email duplicate. This is for the integration of another program (HubSpot) that has to be able to identify and ignore the contacts that are duplicates,.. Could you by chance point me in the right direction for a properly written trigger that would function as needed? – JSlateiv Jul 16 at 17:25
  • @JSlateiv This is not an off-the-shelf function. I would recommend reaching out to your local users' or developers' group for a referral to a local freelance developer who can assist you in completing this project. – David Reed Jul 16 at 17:34
  • Alternately, reach out to HubSpot and request assistance with configuration - I am not familiar with their integration pattern. – David Reed Jul 16 at 17:34
  • No go on HubSpot, their no help in terms of SF config. Can only tell me what needs to happen. I'll reach back out to a local dev group. thanks for the help – JSlateiv Jul 16 at 18:04
  • @JSlateiv - see Hubspot KB article on dups - basically, Hubspot will take (at random) the first SFDC Contact with matching email. HS really wants you to dedup (merge) dups in SFDC before they sync to HS – cropredy Jul 16 at 20:11

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.