0

I have to automate the logic with Trigger. If the Comment__c field is updated in 1 Tool__c record, it should be updated in all another Tool__c records. It's about 700-1000 actual records.

When I'm trying to update it with my Trigger, I'm getting the error: Update failed. First exception on row 163 with id a0G1w000001d91sEAA; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = a0G1w000001d91s) is currently in trigger ToolTrigger, therefore it cannot recursively update itself: []. And I'm trying to update the field exactly in the Tool__c with Id from the Error: a0G1w000001d91sEAA.

My code:

    trigger ToolTrigger on Tool__c (before update) {
    List<Tool__c> tools = [SELECT Id, Comment__c FROM Tool__c WHERE EndDate__c >=: DateTime.NOW()];

    for(Tool__c t : Trigger.new){
        if(Trigger.oldMap.get(t.Id).Comment__c != t.Comment__c){
            for(Tool__c  tool : tools){
                tool.Comment__c = tool.Comment__c;
            } 
        }
    }
    update tools;
}

How can I avoid this error and realize my logic?

  • There are a lot of problems with your code. But I see one main problem with your requirements. What about if multiple tools comments are updated at the same time? Wich one should you take? – Martin Lezer Oct 11 at 12:28
  • 1
    There is potentially no last one. When the trigger is called, the Trigger.new variable is a list of updated tools. So potentially multiple tools at the same time. – Martin Lezer Oct 11 at 12:46
  • 1
    The error you're getting is because you have not excluded the currently updated Tool records from your big query, so it's trying to update the Tool that is already being updated. There are other problems, like the trigger running multiple times, that will appear once that is handled, but I suggest starting there. – Jeremy Nottingham Oct 11 at 12:46
  • 2
    Please edit your post to clarify the use case. If the Comment content has to be same for all Tool records, why not just use a formula field that references a Custom Setting? – David Cheng Oct 11 at 14:53
  • 1
    How many tools do you have in the system? This may not be possible inside a trigger because of WHERE EndDate__c >=: DateTime.NOW() returning more than 50k records. Consider batching the logic. – Drew Kennedy Oct 11 at 16:40
1

Put it in an after trigger. And put the check on 'tools' rather than trigger.oldMap.

So, the tools which have already been updated with this new comment, including the original tool that started the process won't be updated again.

trigger ToolTrigger on Tool__c (after update) {
    List<Tool__c> tools = [SELECT Id, Comment__c FROM Tool__c WHERE EndDate__c >=: DateTime.NOW()];
    List<tool__c> updateTools = new List<tool__c>();
    tool__c t = .... get the appropriate tool you want here
    for(tool__c tool :tools){
        if(tool.Comment__c != t.Comment__c){
                tool.Comment__c = t.Comment__c;
                updateTools.add(tool);
            } 
        }
    }
    update updateTools;
}
  • This code would blow up on updateTools.add(tool); because you only declared updateTools. – Drew Kennedy Oct 11 at 16:39
  • Very true - thanks- corrected – eaeaoo Oct 11 at 16:40
  • Thank you. It works :) – Apelsinova Oct 15 at 14:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.