I've created an apex class that simply generates random data. we have created a sort of "checklist", similar to the todomvc project, which has checklist items and checklist assignees (contacts that are assigned to those todos). This class will generate both todos and randomly assign a list of contacts to the various tasks.
My question is, why am I getting hit by SOQL limits? generating 15 tasks puts the SOQL count up to 94/100. It doesn't seem to matter how many users I assign, so that isn't really upping the count. There should only be 5 SOQL hits (there is one not shown here in grabbing csv data from a static resource). All of the hits come when I upsert(tasks). I couldn't find anything about upserts affecting soql limits. Do they?
Can you help me figure out where the SOQL hits are coming from?
Thank you!
public class dataGen {
private static list<map<string,string>> appData {get;set;}
public dataGen(){
//get app data
appData = getMockData();
}
public void generateTasks(string oppId, integer numTasks){
//get users
list<contact> users = new list<contact>();
Contact user1 = new Contact();
Contact user2 = new Contact();
Contact user3 = new Contact();
user1 = [SELECT id FROM Contact where name like 'user1'];
user2 = [SELECT id FROM Contact where name like 'user2'];
user3 = [SELECT id FROM Contact where name like 'user3'];
users.add(user1);
users.add(user2);
users.add(user3);
//create upload tasks
list<task__c> tasks = createTasks(numTasks, oppId);
try{
upsert(tasks);
}catch(DmlException e){
system.debug(e);
}
system.debug(tasks);
list<Task_Contact__c> c = assignUsers(users, tasks);
try{
upsert(c);
}catch(DmlException e){
system.debug(e);
}
system.debug('tasks: '+tasks);
system.debug('task_contacts: '+c);
}
private static list<task__c> createTasks(integer numTasks, string oppId){
list<task__c> tasks = new list<task__c>();
for(integer i=0; i<numTasks; i++){
integer rndIdx = Integer.valueOf(math.floor(math.random()*appData.size()));
map<string,string> mapRecord = appData.get(rndIdx);
task__c task = new task__c();
task.comments__c = mapRecord.get('comments'); //text
task.Opportunity__c = oppId; //lookup(id)
task.Subject__c = mapRecord.get('subject'); //text
tasks.add(task);
}
return tasks;
}
private static list<Task_Contact__c> assignUsers(list<contact> users, list<task__c> tasks){
list<Task_Contact__c> res = new list<Task_Contact__c>();
integer numUsers = users.size();
for(task__C t:tasks){
integer howManyAssignees = Integer.valueOf(math.rint(math.random()*numUsers));
list<contact> assignees = users.clone();
list<contact> assigned = new list<contact>();
for(integer i = 0; i<howManyAssignees; i++){
integer rndIdx = Integer.valueOf(math.floor(math.random()*assignees.size()));
contact chosenAssignee = assignees.get(rndIdx);
assignees.remove(rndIdx);
assigned.add(chosenAssignee);
}
for(contact user:assigned){
Task_Contact__c tc = new Task_Contact__c();
tc.Task__c = t.Id;
tc.Contact__c = user.Id;
res.add(tc);
}
}
return res;
}
//gets csv data from static resource mockAppdata
private static list<map<string, string>> getMockData(){
//... gets mock data from static csv
}
}
Limits.getQueries()
debug statement at different parts of the code might help others to get it more quickly.generateTasks
is getting called