• Vinod Rajput
  • NEWBIE
  • 0 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies

Hi

 

I have 2 objects here

 

1) Account - Parent 

2) Showing - Child

 

When the status on the account is dead, I wanted the status on the Showing record also to be dead.

 

I have created a trigger to do it but I am getting a strange error.

 

Error:Apex trigger UpdateShowingStatus caused an unexpected exception, contact your administrator: UpdateShowingStatus: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id a0KJ0000001CKO5MAO; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ShowingCount: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 001J000000R4YjHIAV; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = 001J000000R4YjH) is currently in trigger UpdateShowingStatus, therefore it cannot recursively update itself: [] Trigger.ShowingCount: line 21, column 1: []: Trigger.UpdateShowingStatus: line 17, column 1

 

I am posting the 2 triggers here

 

 

trigger UpdateShowingStatus on Account (before update) {
List<ID> accIds = New List<ID>();

  for(Account a:Trigger.new)
{
    if(a.pb__Status__c == 'Dead'){
      accIds.add(a.Id);
    }
  }

  List<pb__Showing__c> s = [ SELECT id,Account__c,Status__c from pb__Showing__c where Account__c = :accIds];
  for(integer i = 0 ; i < s.size(); i++){
    s[i].Status__c =  'Dead';
    
  }

  update s;
}

 

This is another trigger which is causing the conflict

 

trigger ShowingCount on pb__Showing__c (before insert,before update) {
    
    // if(trigger.new[0].Account__c!=null)
    // {
    Account ac=[select id, No_of_Viewings__c from Account where id=:trigger.new[0].Account__c];
    
    //if(ac!=null)
    //{
    if(ac.No_of_Viewings__c == null)
    ac.No_of_Viewings__c =0;
        
        if(trigger.isinsert) {
            ac.No_of_Viewings__c +=1;
        }
    
        if(trigger.isupdate ) {
            if(trigger.new[0].Status__c == 'Cancelled')
            ac.No_of_Viewings__c -= 1; 
        }
    
    update ac;
  
}

 

Can someone help me to fix this error please.

 

Thanks and Kind Regards

 

Finney

  • January 15, 2013
  • Like
  • 0