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
EvertonSzekeresEvertonSzekeres 

System.LimitException: Too many SOQL queries: 101 Data Loader Issue

I have an issue with dataloader while trying to load some records that fires the following trigger that gives me an error System.LimitException: Too many SOQL queries: 101 because of this issue:
trigger AtualizarAR_timba_task on Case (after update) {

List<String> ids = new List <String> ();
    for (Case c: trigger.new) {
        if (Trigger.oldMap.get(c.id).Endere_o_pagamento__c != c.Endere_o_pagamento__c || Trigger.oldMap.get(c.id).Novas_altera_es__c != c.Novas_altera_es__c || Trigger.oldMap.get(c.id).Nome_completo_pagamento__c != c.Nome_completo_pagamento__c && (c.motivo__c == 'Formandos AR')){
            ids.add(c.id);
        }
        
        Map<String, List<Task>> taskMap = new Map<String, List<Task>>();
for (Task t: [SELECT Id, whatid, subject, status, Aprovado__c, description FROM Task WHERE subject = 'AR - ImpressĂŁo aprovada?' AND status != 'ConcluĂ­do' AND whatid =: ids]){

  List<Task> taskList= new List<Task>();
  
  if (taskMap.containsKey(t.whatid)){
    taskList = taskMap.get(t.whatid);
  }
        if(c.Novas_altera_es__c != null){       
            t.Aprovado__c = 'NÃO';
            t.status = 'ConcluĂ­do';
        }
        if(c.Nome_completo_pagamento__c != null && c.Endere_o_pagamento__c == null){
            t.description = 'Nome da lombada = '+c.Nome_completo_pagamento__c+'';
            t.Aprovado__c = 'SIM';
            t.status = 'ConcluĂ­do';
        }
        if(c.Nome_completo_pagamento__c != null && c.Endere_o_pagamento__c != null){

            t.description = 'Nome da lombada = '+c.Nome_completo_pagamento__c+'\n\nNovo endereço = '+c.Endere_o_pagamento__c+'';
            t.Aprovado__c = 'SIM';
            t.status = 'ConcluĂ­do';
          
        }
         taskList.add(t);
         taskMap.put(t.whatId, taskList);
         update(taskList);
}            
}
}
I'll be glad if you can help me with rephrasing the SOQL query to be more efficient.

Thanks,

James LoghryJames Loghry
At Line 09, you are querying inside a for loop.  Once you hit 100+ records in your trigger, it'll throw this exception.

You need to bulkify your trigger by moving the SOQL query outside of your for loop.

See this post for a nice example of how to go about bulkifying your trigger: http://sfdc.arrowpointe.com/2008/09/13/bulkifying-a-trigger-an-example/
Vatsal KothariVatsal Kothari
Hi Everton,

You can refer below code:

trigger AtualizarAR_timba_task on Case (after update) {

	List<Task> taskList= new List<Task>();
	Map<Id,Case> caseMap = new Map<Id,Case>();
	
	for (Case c: trigger.new) {
		if (Trigger.oldMap.get(c.id).Endere_o_pagamento__c != c.Endere_o_pagamento__c || Trigger.oldMap.get(c.id).Novas_altera_es__c != c.Novas_altera_es__c || Trigger.oldMap.get(c.id).Nome_completo_pagamento__c != c.Nome_completo_pagamento__c && (c.motivo__c == 'Formandos AR'))
		{
			caseMap.put(c.id,c);
		}

	}	
	
	for (Task t: [SELECT Id, whatid, subject, status, Aprovado__c, description FROM Task WHERE subject = 'AR - ImpressĂŁo aprovada?' AND status != 'ConcluĂ­do' AND whatid IN: caseMap.keyset()]) {
		if(caseMap.containsKey(t.whatid)){
			if(caseMap.get(t.whatid).Novas_altera_es__c != null){       
				t.Aprovado__c = 'NÃO';
				t.status = 'ConcluĂ­do';
			}
			if(caseMap.get(t.whatid).Nome_completo_pagamento__c != null && caseMap.get(t.whatid).Endere_o_pagamento__c == null){
				t.description = 'Nome da lombada = '+c.Nome_completo_pagamento__c+'';
				t.Aprovado__c = 'SIM';
				t.status = 'ConcluĂ­do';
			}
			if(caseMap.get(t.whatid).Nome_completo_pagamento__c != null && caseMap.get(t.whatid).Endere_o_pagamento__c != null){

				t.description = 'Nome da lombada = '+c.Nome_completo_pagamento__c+'\n\nNovo endereço = '+c.Endere_o_pagamento__c+'';
				t.Aprovado__c = 'SIM';
				t.status = 'ConcluĂ­do';
			}
		}
		taskList.add(t);
	}
	
	if(taskList.size() > 0){
		update taskList;
	}
}

If this solves your problem, kindly mark it as the best answer.

Thanks,
Vatsal