• Anish Kumar 70
  • NEWBIE
  • 0 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 2
    Replies
I have a trigger that has to delete an Account record associated with a Contact record when that particular contact  record gets deleted, However I get the following error when the DML  statement delete get's executed.

"15:14:59:227 FATAL_ERROR System.DmlException: Delete failed. First exception on row 0 with id 0011100000R5Gf0AAF; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = 0031100000QId7U) is currently in trigger deleteAccountRecord, therefore it cannot recursively delete itself: []"

here is my code: 

trigger deleteAccountRecord on Contact (before delete) {


    RecordType donorRecType = [SELECT ID FROM RecordType WHERE sObjectType = 'Contact' AND DeveloperName = 'Donor_Contact'];
    if(Trigger.isDelete){
        List<ID> contactIDList = new List<ID>();
        for(Contact contactToDelete: Trigger.old) {
            if (contactToDelete.RecordTypeId != donorRecType.Id ) {
                  
                    contactIDList.add(contactToDelete.id);
            }
        }
      
       
       List<Account> DelAccountRecordList = [select id from Account where Id IN(SELECT accountId FROM Contact WHERE Id IN :contactIDList)];

        if(DelAccountRecordList.size() >0 && RecursiveTriggerHelper.isFirstRun){
           RecursiveTriggerHelper.isFirstRun = false;
           delete DelAccountRecordList ;
         
        }
     }
  }

//Helps to stop the trigger from firing twice. (at least It should but it's not working)
public class RecursiveTriggerHelper {

     public static boolean isFirstRun = true;
  
}

I have tried using "After Insert" but DelAccountRecordList returns null with no records. Is there a way to deal with my error without changing "Before Update". I am totally out of ideas on how to solve this issue.

Thanks in advance for your help.
1) Enable account team member
follow navigation  setup==>customize==>account====>Account teams .
once you enable this accountteam member object is added to related list of account.

2) create one check box VPA on account object.

now the requirement is:


create one record in account (call parent record) mention VPA enable and don't select parent look up field.

create  one more record in account(call child) uncheck VPA and select (parent record) as parent for this child record using look up.

here VPA is enable means that record is parent other wise it's a child record...

Need to be achieve:

1) once you create a child record then owner of parent record should be add to child   related accountteam member.(parent owner should be insert as accounteam meber to child)

2) if you change the owner in parent it should replace the old owner with new onwer.