0

Getting an error in Line 8 in the query in the constructor contactFindNextLastVisit. Can someone see what that might be? There are no SOQL statements in for loop.

public class ConNextEventUpdate_EventTriggerHelper {
    public static void contactNextEventUpdate(List<Event> newEvents, List<Event> oldEvents, boolean isInsert, boolean isUpdate, boolean isDelete, boolean isUndelete)
    {

        //find contacts associated to the events that were created, deleted, or updated (both old and new values)
        Set<ID> conIDs = new Set<ID>();

        //get contact ids for newly inserted/updated record
        if (isInsert || isUpdate || isUndelete) {
            for (Event e : newEvents) {
                if (e.WhoId != null && e.Group_Event_Lookup__c == Null) {
                    //a WhoId starting with '003' is a contact
                    if ( String.valueOf(e.WhoId).startsWith('003') ) {
                        system.debug('Inside First If-----------------------contactLastNextVisitUpdate');
                        conIDs.add(e.WhoId);
                    }
                }
            }
        }

        //get contact ids for deleted record or previous value of updated record
        if (isDelete || isUpdate) {
            for (Event e : oldEvents) {
                if (e.WhoId != null && e.Group_Event_Lookup__c == Null) {
                    //a WhoId starting with '003' is a contact
                    if ( String.valueOf(e.WhoId).startsWith('003') ) {
                        system.debug('Inside Second If-----------------------contactLastNextVisitUpdate');
                        conIDs.add(e.WhoId);
                    }
                }
            }
        }


        //now that we have our contact ids, we need to get the contact records and all events associated to those contacts  
        /*
        Publish date for next or last “qualifying event”
        -  Next Visit 
        -  Last visit

        Definition of “qualifying Event”:
        “Cancelled” not equal to True
        “DM not met” not equal to True
        Events that are related to one account but have less than four attendees (contacts) associated to the same event.  
        Type = Visit activities
        */
        List<Contact> cons2Update = new contactFindNextLastVisit(conIDs).cons2Update;
        system.debug('List cons2Update: '+cons2Update);
        system.debug('cons2Update.size(): '+cons2Update.size());
        //update the contacts
        if (cons2Update.size() > 0) {
            update cons2Update;
        }

    }
}

public class contactFindNextLastVisit {

    public List<Contact> cons2Update {get;set;}

    //constructor
    public contactFindNextLastVisit(Set<ID> conIDs) {
        system.debug('conIDs.size():' +conIDs.size());
        List<Event> lstEvents = [Select Id
                                    , WhoId
                                    , Event_Type__c
                                    , ActivityDate
                                    , (Select Id, EventId, AttendeeId From EventAttendees) 
                                From Event
                                Where WhoId in :conIDs
                                    AND Event_Type__c = 'Visit activities' 
                                    AND Cance_lled__c = false 
                                    AND DM_not_met__c = false 
                                    AND Group_Event_Lookup__c = Null
                                Order By WhoId, ActivityDate];

        //loop through each contact and find the activities for it
        Boolean bEventContactFound;
        Boolean bEventContactUpdated;
        Date dtLastVisit;
        Date dtNextVisit;
        Date dtLastVisitDefault = date.newInstance(1980, 1, 1); 
        Date dtNextVisitDefault = date.newInstance(2200, 1, 1);

        this.cons2Update = new List<Contact>();     
        system.debug('conIDs.size(): '+conIDs.size());
        List<Contact> conList = [Select Id, Next_Visit__c, Last_Visit__c From Contact 
                            Where Id in :conIDs 
                            Order By Id];

        for (Contact c : conList) {

            //reset variables as we start a new contact
            bEventContactFound = false;
            bEventContactUpdated = false;
            dtLastVisit = dtLastVisitDefault;
            dtNextVisit = dtNextVisitDefault;

            //loop through all events and find those linked to the current contact

            for (Event e : lstEvents) {
                if (e.WhoId == c.Id) {
                    bEventContactFound = true;
                    if (e.EventAttendees.size() < 4) {
                        if ( (e.ActivityDate > dtLastVisit) && (e.ActivityDate <= date.today()) ) { 
                            dtLastVisit = e.ActivityDate;
                        }
                        if ( (e.ActivityDate < dtNextVisit) && (e.ActivityDate > date.today()) ) { 
                            dtNextVisit = e.ActivityDate;
                        }
                    }
                } else if (bEventContactFound) {
                    //we've already processed the contact so we can quit the loop
                    break;
                }
            }

            //set the last visit value on the contact
            if (c.Last_Visit__c <> dtLastVisit) {
                bEventContactUpdated = true;
                if (dtLastVisit == dtLastVisitDefault) {
                    c.Last_Visit__c = null;
                } else {
                    c.Last_Visit__c = dtLastVisit;
                }
            }

            //set the next visit value on the contact
            if (c.Next_Visit__c <> dtNextVisit) {
                bEventContactUpdated = true;
                if (dtNextVisit == dtNextVisitDefault) {
                    c.Next_Visit__c = null;
                } else {
                    c.Next_Visit__c = dtNextVisit;
                }
            }

            //add the contact to our update list
            if (bEventContactUpdated) {
                this.cons2Update.add(c);
            }

        }

    }

}
4
  • 4
    What other code and automation is running on this object?
    – David Reed
    May 6, 2019 at 14:10
  • 4
    Constructor is meant to creating an instance of an class. Only for initialing values are recommended. Don't use all the SOQL and other logic in constructor May 6, 2019 at 14:16
  • 2
    If this is an after trigger, then update cons2Update may set off recursion - that's where I'd start looking first May 6, 2019 at 15:05
  • 1
    In my very own opinion, it seems like a bad practice to have all that logic inside the constructor, I believe that a constructor should work to give an object a default structure and values if is the case, in that case, is good also to have a dummy constructor. Why don't you try to take out the variables, the SOQL, and the logic out of the constructor and build methods instead? I'm not an expert but to me, that could work.
    – AjFmO
    May 6, 2019 at 16:02

1 Answer 1

0

Since the code you provided does not have SOQL statements in loops, the code itself must be called in a loop. You should provide us with the logic that invoked contextNextEventUpdate(...). My guess would be, that your logic somehow invokes the whole function in a loop.

You should start by executing the unit, opening the debug logs and looking for invocations of contactNextEventUpdate. If it appears multiple times, there's your cause.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .