+ Start a Discussion
lawlaw 

Too many SOQL queries: 101

Hello 

 

I know this usually happens when  you have your SOQL in a loop.  However in the code below I do not see this.  The code is below. I have highlighted the line of SQL in Question.

 

trigger UpdateFieldsFromLogMeeting on Task (after insert, after update) {
SingletonResource singleton = SingletonResource.getInstance();

Id callRecId = singleton.callRecId;
Id meetingRecId = singleton.meetingRecId;
Id faceRecId = singleton.faceToFaceRecId;
Id prospectRecId = singleton.prospectRecId;
Id candidateRecId = singleton.candidateRecId;
Id sysAdminId = singleton.sysAdminId;
list<Id> ContactIds = new List<Id>();
list<Id> OwnerIds = new List<Id>();

System.Debug('Go through tasks');
for(task t : Trigger.new) {
ContactIds.add(t.WhoId);
System.Debug('Task Owner: ' + t.OwnerId);
OwnerIds.add(t.OwnerId);
}

//------------------------------------------------
map<id,contact> contacts = new map<id,contact>();
for(task record:trigger.new) {
if(record.whoid!=null&&record.whoid.getsobjecttype()==contact.sobjecttype) {
contacts.put(record.whoid,null);
}
}
contacts.putall([select Id,Phone,FirstName,Email, EAR_Assist__c, EAR_Assist_Name__c, EAR_Validate__c, EAR_Validation_Date__c, LeadSource, EARAssistTask__c from contact where id in :contacts.keyset()]);
//------------------------------------------------
map<id,contact> contactstoupdate = new map<id,contact>();
Map<Id,User> users = new Map<Id,User>();

for(User u : [Select id, Name, UserRole.Name from User u where u.id in :OwnerIds]) {
System.Debug('UserRole.Name loop:' + u.userrole.name);
users.put(u.id, u);
}

for(task t:trigger.new) {


if (t.Status == 'Completed' && (t.Type == 'Face-to-Face Meeting' || t.Type == 'Follow Up') &&
t.RecordTypeId != callRecId){
//Select the contact associated with the new Meeting
//system.debug ('VALUE OF I = ' + i);

Contact c = contacts.get(t.whoid);
if(c==null) {
continue;
}


//Update fields in the contact from values in the Meeting Logged
c.Viability__c = t.Viability__c;
c.Estimated_T_12__c=t.Estimated_T_12__c;
c.Estimated_AUM__c=t.Estimated_AUM__c;


String roleName = '';

if ( users.containsKey(t.Ownerid) ) {
User u = users.get(t.Ownerid);
// System.Debug('User ' + u.Name);
roleName = u.UserRole.Name.toLowerCase();
// System.Debug('ROLE ' + roleName);

} else {
System.Debug('Owner Not Found');
}

if ( roleName.contains ('regional dir-ear')) {
System.Debug('Role Name = ' + roleName);
if (c.OwnerId != sysAdminId ) {
c.EAR_Assist__c = true;
c.EAR_Assist_Name__c = t.OwnerId;
c.EARAssistTask__c = t.id;

if ( c.LeadSource == 'Self-Sourced') {
c.EAR_Validate__c = true;
c.EAR_Validation_Date__c = t.ActivityDate;
}
} else if ( c.EAR_Assist_Name__c == t.Ownerid ) {
c.EAR_Validate__c = true;
if (c.EAR_Validation_Date__c == null || t.ActivityDate > c.EAR_Validation_Date__c) {
c.EAR_Validation_Date__c = t.ActivityDate;
}
}

}


contactstoupdate.put(c.id,c);

}

}

if ( contactsToUpdate.size() != 0) {
update contactstoupdate.values();
}
}

sfdcfoxsfdcfox
This code is just fine. The problem is elsewhere. When does this error appear? If you're using a Visualforce page, it might be exhausting the SOQL limit before you insert the task records. You'll have to check your debug logs (set profiling to finest to see which queries execute most often in the logs).
lawlaw

This occurs when the owner of a contact is changed on the contact page.  The contact has several  activities associated with it.

It is my understanding that when you change the owner of a contact that has activities associated with it, SF does and update/edit on this contact for each activity?

sfdcfoxsfdcfox
I know that open activities are transferred with the contact, and since it appears that task/event triggers execute for these tasks as they move ownership, I'd suggest looking at the contact triggers to see what's going o.
sushant sussushant sus
hi,

you can limit in this query or call this from aclass
create a global class method that return map that we can use in ur trigger .
this will help you ...
lawlaw

Could you give me some sample code of this?