function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
KDPKDP 

Too many SOQL statements from test class, trigger OK!!!

So, I've written triggers to set the value of a case owner's department field based on the queue name or the user department.  The triggers work just fine, including when I did a dataload to populate a different field.  Post upsert extract showed that the upsert initiated the trigger, which populated that department field for 68k records.

 

Unfortunately, I'm having problems getting the test class to complete.  It is returning a too many soql statements during a map statement that is before any processing loops.

 

The too many SOQL statements is occurring at the second map statement.

 

Trigger:

 

trigger Case_SetOwnerDept_Update on Case (before update) {
// When 'Owner' field is changed, update 'Case Owner's Department' too
Set <String> sUserIDs = new Set<String>();
Set <String> sQueueIDs = new Set<String>();
Set <String> sOwnerIDs = new Set<String>();

Map <String, User> mUsers = new Map<String,User>([Select ID, Department from User where ID in:sUserIDs]);
Map <String, Group> mGroups = new Map<String,Group>([Select ID, Name From Group where type='Queue' and ID in :sQueueIDs]);
Map<ID, Case> mCases = new Map<ID, Case>();

system.debug('mUser size: ' + mUsers.size());
system.debug('mGroupgs size: ' +mGroups.size());

For (Case c :trigger.new){
    String sOwner = c.ownerid;
    if(sOwner.substring(0,3)=='005'){
        sUserIDs.add(c.OwnerID);
    } else {
        sQueueIDs.add(c.OwnerID);
    }
}


//Now run through trigger again to populate department
For (Case c :trigger.new){
        Case oldCase = trigger.oldMap.get(c.Id);
        string oOwner = oldCase.Ownerid;
    if (c.ownerid != oldCase.ownerid){
        mCases.put(c.OwnerId, c);
    }
    String sOwner = c.ownerid;
        system.debug ('Old Case owner id: '+oOwner);
        system.debug ('New Case Owner id: '+sOwner);   
  {  
    if(sOwner.substring(0,3)=='005'){
        //String sOwner = c.OwnerID;    
        User oUser = mUsers.get(sOwner);
        if(oUser!=null){
            c.Case_Owners_Department__c=oUser.Department;
            system.debug('Set Owner Department user department: ' + c.Case_Owners_Department__c);
        }
    } else {
        String sGroup = c.OwnerID;
        Group oGroup = mGroups.get(sGroup);
        if(oGroup!=null){
            c.Case_Owners_Department__c=oGroup.name;
            system.debug('Set Owner Department as queue name: ' + c.Case_Owners_Department__c);
        }
    }
}
}
}

 

Any thoughts would be appreciated since I was scheduled to put this into production today, and if I can't work this out...

dmchengdmcheng

I'm surprised that your trigger is working.  You have not populated the sUserIDs and sQueueIDs sets before the map statements, so it seems to me that the first two maps should end up empty.

 

Also, out of curiosity - is there a reason you are using String type instead of ID data type for IDs?