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
HARSHIL U PARIKHHARSHIL U PARIKH 

Simple Lookup relationship trigger is not working when you take off the parent object name from child? (Otherwise its works fine!)

Hello Developers,

I am running into this following situation:

I have a trigger on Cases which updates Total_Cases_Count__c on Account and Contacts. It works fine for insert, update, and all delete and undelete conditions such as when I insert case with account named "Appolo" and Contact named "Mark" then both "Appolo" and "Mark" will have Total_Cases__c as 1. But however, when I just take the contact "Mark" off from that case then under the Mark's contact record Total_Cases__c is still 1 even though mark doersn't have any cases under the related list anymore.
Thank you for your help!

Code:
Trigger CaseRecordCount on Case(After Insert, After Update, After Delete, After UnDelete){
    
    List<ID> AccountIds = New List<ID>();
    List<ID> ContactIds = New List<ID>();
    
    If(Trigger.IsInsert || Trigger.IsUpdate || Trigger.IsUnDelete){
        For(Case c: Trigger.New){
            AccountIds.add(c.AccountId);
            ContactIds.add(c.ContactId);
        }
     }
    If(Trigger.IsDelete){
        For(Case c: Trigger.Old){
            AccountIds.add(c.AccountId);
            ContactIds.add(c.ContactId);
        }
     }
     
     List<Account> AccountListToUPdate = New List<Account>();
     List<Contact> ContactListToUpdate = New List<Contact>();
     
     For(Account act: [Select Total_Cases__c, (Select ID FROM Cases) FROM Account WHERE ID = :AccountIds]){
     act.Total_Cases__c = act.Cases.size();
     AccountListToUpdate.add(act);
     }
     
     For(Contact con: [Select Total_Cases__c, (Select ID FROM Cases) FROM Contact WHERE ID = :ContactIds]){
     con.Total_Cases__c = con.Cases.size();
     ContactListToUpdate.add(con);
     }
     
     try{
         Update AccountListToUpdate;
         Update ContactListToUpdate;
     }
     Catch(Exception E){
         System.Debug('Error Message: ' + e.getMessage());
     }
    

}

 
Best Answer chosen by HARSHIL U PARIKH
HARSHIL U PARIKHHARSHIL U PARIKH
Tarun You are correct!
I have actually handled this situation with standard functionalities and checked it's working fine from my side.

If anyone is reviewing this post then here are the steps I have taken to resolve the issue.
1) Created a previous contact field on contact record which is lookup to contact and read only for all profiles and not in the layout even.
2) Created a process builder which fires process when there is a change in contact then previous contact becomes takes the old value of that just changed contact field. (e.g., if original case created with john as contact then previous contact will be blank, if you change john to mike then previous contact becomes john)
3) Created another process which fires every time record is created or edited + when there is value in previous contact field + when previous value of 'previous contact' field is different then current value of 'previous contact' field. Once the process is fired, go and update 'total cases' to total cases -1 for the previous contact. (in our example above it should go to the john's conatct record and make total case = total case - 1.

Thank You!
 

All Answers

Tarun J.Tarun J.
Hello Govind,

If you are updating the case record and removing Contact value (Making contact field on Case blank or transfer to some other contact), ContactId field on Case record will not give Mark's contact Id. In case of removing, ContactId will be blank. If you have updated it to some other contact, ContactId will contains new contact Id.

In both cases, you query on contact will not return Mark record and hence, it won't get updated.

You need to update your logic to handle both scenarios using Trigger.Old and check if Contact field is blank or updated, you need to recalculate 'Total_Cases_Count__c ' on those contacts.

-Thanks,
TK

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.
HARSHIL U PARIKHHARSHIL U PARIKH
Tarun You are correct!
I have actually handled this situation with standard functionalities and checked it's working fine from my side.

If anyone is reviewing this post then here are the steps I have taken to resolve the issue.
1) Created a previous contact field on contact record which is lookup to contact and read only for all profiles and not in the layout even.
2) Created a process builder which fires process when there is a change in contact then previous contact becomes takes the old value of that just changed contact field. (e.g., if original case created with john as contact then previous contact will be blank, if you change john to mike then previous contact becomes john)
3) Created another process which fires every time record is created or edited + when there is value in previous contact field + when previous value of 'previous contact' field is different then current value of 'previous contact' field. Once the process is fired, go and update 'total cases' to total cases -1 for the previous contact. (in our example above it should go to the john's conatct record and make total case = total case - 1.

Thank You!
 
This was selected as the best answer