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

two triggers conflict!!

Hello everyone!!


I have two triggers, the first one;

trigger DisparaHistoricoInmobiliario on Task (before insert, before update) {
 Integer i=0,j=0;
            for(Activo_Inmobiliario__c AI:[select trigger__c from Activo_Inmobiliario__c where Fecha_de_Venta__c=null or Fecha_de_Venta__c=YESTERDAY])
                update AI;





and the second one:

trigger Actualizar_Lista_Sharing_ActInm on Activo_Inmobiliario__c (after update)
    for (Activo_Inmobiliario__c
        for (Activo_Inmobiliario__c embargoOLD:trigger.old)
            for(Activo_Inmobiliario__Share aShare:[select RowCause,UserOrGroupId,AccessLevel from Activo_Inmobiliario__Share where ParentId=:EmbargoNEW.Id])
                     update aShare;
                Activo_Inmobiliario__Share aShareAI=[select RowCause,UserOrGroupId,AccessLevel from Activo_Inmobiliario__Share where ParentId=:EmbargoNEW.Id and UserOrGroupId=:EmbargoNEW.Agente_Inmobiliario__c];
                 update aShareAI;





OK! This is the problem;


When both of them are fired, I get this error message:


Apex script unhandled trigger exception by user/organization: 00580000001kVyW/00D80000000cPxj

DisparaHistoricoInmobiliario: execution of BeforeUpdate

caused by: System.DmlException: Update failed. First exception on row 0 with id a008000000D0QZmAAN; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Actualizar_Lista_Sharing_ActInm: execution of AfterUpdate

caused by: System.Exception: Too many SOQL queries: 21

Trigger.Actualizar_Lista_Sharing_ActInm: line 7, column 45: []

Trigger.DisparaHistoricoInmobiliario: line 11, column 17






I think It's because the first trigger should wait before each update, to let the second trigger to do his part!!


Is there any way to let them work in parallel?


Thanks in advance!!


Your error message actually is indicating too many SOQL calls are getting made during the execution.  This is because in both triggers, you have a SOQL statement inside your for loop.  In your second trigger, it is inside of a nested loop.

You need to refactor your code to move those queries outside of the loop, and leverage a map to hold the data.

You also have an update DML inside of the for loop, which will cause you to hit the DML limit as well.


I am not sure about the logic flow in your first trigger either.

If I understand it correctly, for each time you find a task with the field trigger__c==1 you loop through all of the Activo_Inmobiliario__c where Fecha_de_venta__c=null or Fecha_de_Venta__c=YESTERDAY

then you update 2 fields on the Activo_Inmobiliario__c record,

but not with anything related to the task at the beginning of the loop.

As I see it, if you found 3 matching tasks, you would update all of the Activo_Inmobiliario__c that matched your SOQL filter 3 times with the same information.






You have queries and DML statements inside loops in both triggers. This leads running into governor limits when processing batch of records.


See (especially #1 and #2).


You can also find more from Apex developers guide by searching for 'bulk triggers' and 'governor limits' for example.