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
virkvirk 

Update count after a custom object is inserted or deleted

I want to update a count on Contact object after a new record is inserted or deleted on Lents object. My code is working fine to update the count by 1 but is not decreasing its value by 1 please help me:

 

trigger UpdateCount on Lent__c (after insert,after delete) {
List<Contact> counts_toUpdate = new List<Contact>();
Map<String, Integer> contact_newCount_Map=new Map<String, Integer>(); //for each contact the CountNumber

if(trigger.isinsert){
List<Lent__c> counts =[select Contact__c, Contact__r.Count__c
from Lent__c where id IN :Trigger.new FOR UPDATE];
for(Lent__c lent:counts){
contact_newCount_Map.put(lent.Contact__c, Integer.valueOf(lent.Contact__r.Count__c==null ? 0 : lent.Contact__r.Count__c) + 1);
}
}
else{
Set<Id> contactIds = new Set<Id>();
for (Lent__c l : Trigger.old)
contactIds.add(l.Contact__c);
List<Lent__c> counts = [select Contact__c, Contact__r.Count__c
from Lent__c where id IN :contactIds];
Map<Id,Contact> oldContacts = new Map<Id,Contact>([SELECT Count__c From Contact where Id IN :contactIds]);
for(Lent__c lent : counts)
contact_newCount_map.put(lent.Contact__c, Integer.valueOf(oldContacts.get(lent.Contact__c).Count__c == null ? 0 : oldContacts.get(lent.Contact__c).Count__c ) -1);
}

for(Contact con : [select id, Count__c from Contact WHERE ID IN : contact_newCount_Map.KeySet()]){
con.Count__c=contact_newCount_Map.get(con.id);
counts_toUpdate.add(con);
}

update counts_toUpdate;
}

virkvirk

CAn someone please help its very urgent I will be very thankful.

SKiranSKiran
Gone through the code, but I didn't find anything. One solution could be having a roll-up summary on contact to get the count of child objects which is Lent in your case. That would be an elegant solution in Salesforce. Note: roll-up summary works only for master detail relationships.
virkvirk
Thanks a lot for the reply for rollup summary fils what i need to do?
SKiranSKiran
Please hit star and accept it as solution so that other could get benefited..
cplusplus_pleasecplusplus_please
I think he's still unsure of your answer :P



=============================
Developer for www.voicent.com
Voicent provides CRM, auto dialer, predictive dialer, reminder, and call center solution that is suitable for anyone in any level
Voicent strives to meet customer demands to integrate Voicent software with CRM of their choice.