+ Start a Discussion
Apex EstrategiaApex Estrategia 

Close task and close case

Hi !

 

I create a trigger to close my case when I close one specific task.

 

trigger FechaTask_FechaCase on Task (after update) {

Task[] cse = Trigger.new;

    Set<ID> cseIds = new Set<ID>();
    for (Task t:cse)
      if (t.status == 'Completed' && t.subject == 'Baixar PP')
      cseIds.add(t.whatid);

      for (Case c : [select id, motivo__c, status from case where id in :cseIds for update])
      if (c.motivo__c == 'Solicitação de Pagamento'){
        c.status = 'Closed';
        update c;
      }
    }

 

And I got an error:

 

  • Corpo: Validation Error: Value is required
  • FechaTask_FechaCase: System.LimitException: Too many SOQL queries: 101

 

I don't know why this error appears just when I try to close the case.

If I update a regular field my trigger works.

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox
Your code isn't bulk safe, so you might be triggering other triggers that are performing a large number of queries, causing the error you describe. Other than that, I don't see a reason why this code specifically here should fail, so it's most likely a red herring. You'll have to run through your debug logs and find out where the most queries are being executed; most likely there is other code out there that is also not bulk-safe and is causing this particular trigger to fail.

All Answers

sfdcfoxsfdcfox
Your code isn't bulk safe, so you might be triggering other triggers that are performing a large number of queries, causing the error you describe. Other than that, I don't see a reason why this code specifically here should fail, so it's most likely a red herring. You'll have to run through your debug logs and find out where the most queries are being executed; most likely there is other code out there that is also not bulk-safe and is causing this particular trigger to fail.
This was selected as the best answer
Apex EstrategiaApex Estrategia

Thanks sfdcfox.

It's what I thought.

 

I have one trigger to close some tasks if I close the case.

I think they are in conflict.

 

Thanks again.

I will not use this trigger anymore. It's not as important.