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
javierjiesjavierjies 

Trigger doesn't work

Hello everyone!!

 

I've donde this Trigger:

 

trigger FacturacionAutomatica on Expediente__c (after update)
{
   
    Factura__c fac=new Factura__c();
    Tarifa__c tar1;
    for(Expediente__c exp:trigger.new)
    {
        tar1=[select Corresponde__c,Restar_Honorario__c,PSobre_Ingresos__c,Importe_Rango_Superior__c,
              Sobre_Ingresos_Superior_al_Rango__c,Abono_sobre_Facturado__c from Tarifa__c
              where Situacion__c=:exp.Situacion__c
              and Tipo_Demanda__c=:exp.Tipo_Demanda__c
              and Expediente_Cerrado__c=:exp.Cerrado__c
              and Cantidad_Hasta__c>=:exp.Reclamada__c
              and CantidadDesde__c<=:exp.Reclamada__c];         
        fac.Honorarios__c=tar1.Corresponde__c;
        fac.Expediente__c=exp.Id;
        fac.RecordTypeId='012300000003peBAAQ'; 
        insert fac;
    }
}

 

 

 

When I update Expediente__c I get this error message:

 

Error:Apex trigger FacturacionAutomatica caused an unexpected exception, contact your administrator: FacturacionAutomatica: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, FacturacionAutomatica: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, FacturacionAutomatica: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, FacturacionAutomatica: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, FacturacionAutomatica: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, FacturacionAutomatica: execution of AfterUpdate caused by: System.Exception: Too many SOQL queries: 21 Trigger.FacturacionAutomatica: line 8, column 14 Trigger.FacturacionAutomatica: line 18, column 9 Trigger.FacturacionAutomatica: line 18, column 9 Trigger.FacturacionAutomatica: line 18, column 9 Trigger.FacturacionAutomatica: line 18, column 9: Trigger.FacturacionAutomatica: line 18, column 9

 

 

I don't understand why It doesn't work, because I've done similar Triggers and they run perfectly

 

so..any idea??

 

Thanks in advice!!

Best Answer chosen by Admin (Salesforce Developers) 
Mikko4Mikko4

Hi,

 

You shouldn't query a table without filters --> System.Exception: Too many query rows: 1001 and you have a query inside a loop.

 Try something like:

 

List<Id> situacions = new List<Id>();

for(Expediente__c exp:trigger.new)
{

    situacions.add(exp.Situacion__c); 

}

List<Tarifa__c> tarifas = [select Situacion__c from Tarifa__c where Id in :situacions];

List<Factura__c> facturas = new List<Facturas__c>();

for(Expediente__c exp:trigger.new)

{

    for(Tarifa__c tar : tarifas)

    {

         if(exp.Situacion__c == tar.Situacion__c) {

             Factura__c fac=new Factura__c();

             fac.Honorarios__c=123;

             facturas.add(fac);

             break;

         }

    }

}

insert facturas;

 

 

I didn't check what I wrote so I don't guarantee that it's flawless ;) Also I'm not sure if I understood the logic you are trying to build.

But this should give you idea how to create triggers that support bulk processing.

All Answers

ShikibuShikibu

For one thing, your trigger is executing one select and one insert per record in trigger.new. Apex triggers are permitted only 20 DML statements. If there are 11 items in trigger.new, your trigger will fail. 

 

Have a read on the Bulk Trigger Idioms page of the Apex User Guide. 

javierjiesjavierjies

I made It smaller

 

trigger FacturacionAutomatica on Expediente__c (after update)
{
   
    Factura__c fac=new Factura__c();
    for(Expediente__c exp:trigger.new)
    {
        for(Tarifa__c tar1:[select Situacion__c from Tarifa__c])
        {
              if(tar1.Situacion__c==exp.Situacion__c)
              {
                  fac.Honorarios__c=123;
                  fac.Expediente__c=exp.Id;
                  fac.RecordTypeId='012300000003peBAAQ'; 
              }
         }
     }
     insert fac;
}
 

 

and I get the same error message,

 

Error:Apex trigger FacturacionAutomatica caused an unexpected exception, contact your administrator: FacturacionAutomatica: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, FacturacionAutomatica: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, FacturacionAutomatica: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, FacturacionAutomatica: execution of AfterUpdate caused by: System.Exception: Too many query rows: 1001 Trigger.FacturacionAutomatica: line 7, column 28 Trigger.FacturacionAutomatica: line 17, column 6 Trigger.FacturacionAutomatica: line 17, column 6: Trigger.FacturacionAutomatica: line 17, column 6

 

 

so, I'm not sure if It's about the number of fields I'm working with...

 

 

Please help, my boss is gonna kill me!!

Mikko4Mikko4

Hi,

 

You shouldn't query a table without filters --> System.Exception: Too many query rows: 1001 and you have a query inside a loop.

 Try something like:

 

List<Id> situacions = new List<Id>();

for(Expediente__c exp:trigger.new)
{

    situacions.add(exp.Situacion__c); 

}

List<Tarifa__c> tarifas = [select Situacion__c from Tarifa__c where Id in :situacions];

List<Factura__c> facturas = new List<Facturas__c>();

for(Expediente__c exp:trigger.new)

{

    for(Tarifa__c tar : tarifas)

    {

         if(exp.Situacion__c == tar.Situacion__c) {

             Factura__c fac=new Factura__c();

             fac.Honorarios__c=123;

             facturas.add(fac);

             break;

         }

    }

}

insert facturas;

 

 

I didn't check what I wrote so I don't guarantee that it's flawless ;) Also I'm not sure if I understood the logic you are trying to build.

But this should give you idea how to create triggers that support bulk processing.

This was selected as the best answer
javierjiesjavierjies
Great!! thank you!!