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
Max Alexander 19Max Alexander 19 

Auto increment child object number

HI,
i hav follwing issue, on every opportunity i have deal offers its a custom object, so what i need to do is to create auto incremetn number on each deal offer idependent of opportunity, so for example
opportunity A has 2 deal offers number for them will be 1, 2
Opportunity B has 3 deal offers numbers for them would be 1,2,3
and so on so counter for deal offers are only applicable within opportunity.
i have this piece of code works fine with all new records, but when i try to insert in bulk or update in bulk it gives me duplicates
trigger rollupChildren on Deal_Offer__c (before insert, before update) {
 set <id> ids = new SET<id>();
  
  
       for(deal_offer__c d:Trigger.new){
       ids.add(d.opportunity__c);
       }
  
List<Deal_Offer__c> doList = [select  id,id__c from Deal_Offer__c where opportunity__c in:ids   order by id__c desc ];
  
  
  for (Deal_Offer__c d:trigger.new){   
        IF(!doList.isempty()){
            d.id__c=doList[0].id__c;
         }
           IF(doList.isempty()){
           d.id__c=1;
           }
  }
  
  
}


any sugestion?
JyothsnaJyothsna (Salesforce Developers) 
Hi Alexander,

Please check the below sample code(number of contacts related to the particular account). 
 
trigger ContactCount on Contact (after insert, after update, after delete) {
    Map<Id, List<Contact>> AcctContactList = new Map<Id, List<Contact>>();
    Set<Id> AcctIds = new Set<Id>();    
    List<Account> AcctList = new List<Account>();
    List<Contact> ConList = new List<Contact>();
    
    if(trigger.isInsert || trigger.isUPdate) {
        for(Contact Con : trigger.New) {
            if(String.isNotBlank(Con.AccountId)){
                AcctIds.add(Con.AccountId);  
            }   
        }  
    }
    
    if(trigger.isDelete || trigger.isUPdate) {
        for(Contact Con : trigger.Old) {
            AcctIds.add(Con.AccountId);     
        }  
    }           
    
    if(AcctIds.size() > 0){
        ConList = [SELECT Id, AccountId FROM Contact WHERE AccountId IN : AcctIds];
        
        for(Contact Con : ConList) {
            if(!AcctContactList.containsKey(Con.AccountId)){
                AcctContactList.put(Con.AccountId, new List<Contact>());
            }
            AcctContactList.get(Con.AccountId).add(Con);      
        }                           
        
        System.debug('Account Id and Contact List Map is ' + AcctContactList);
            
        AcctList = [SELECT No_of_contacts__c FROM Account WHERE Id IN : AcctIds];
        
        for(Account Acc : AcctList) {
            List<Contact> ContList = new List<Contact>();
            ContList = AcctContactList.get(Acc.Id);
            Acc.No_of_contacts__c= ContList.size();
        }    
        
        System.debug('Account List is ' + AcctList);
        update AcctList;    
    }

}


Hope this helps you!
Best Regards,
Jyothsna
Nagendra ChinchinadaNagendra Chinchinada
Try this, it will work for bulk operations too.
 
trigger rollupChildren on Deal_Offer__c (before insert, before update) {

		set <id> ids = new SET<id>();
        Map<Id,List<Deal_Offer__c>> dealoffrMap = New  Map<Id,List<Deal_Offer__c>>();
        Map<Id,integer> dynamicIds = New Map<Id,integer>();
       // integer dynamicId ;
        
        
        for(deal_offer__c d:Trigger.new){
            ids.add(d.opportunity__c);
        }
        
        //List<Deal_Offer__c> doList = [select  id,id__c,opportunity__c from Deal_Offer__c where opportunity__c in:ids  ];
        
        for(Deal_Offer__c dof : [select  id,id__c,opportunity__c from Deal_Offer__c where opportunity__c in:ids  ]){
            
            if(dealoffrMap.containsKey(dof.opportunity__c)){
                List<Deal_Offer__c> temp =  dealoffrMap.get(dof.opportunity__c);
                temp.add(dof);
                dealoffrMap.put(dof.opportunity__c,temp);
            }
            else{
                List<Deal_Offer__c> temp =  new List<Deal_Offer__c>();
                temp.add(dof);
                dealoffrMap.put(dof.opportunity__c,temp);
            }
            
        }
        
        for(Id OppIds :dealoffrMap.keySet() ){
            List<Deal_Offer__c> dofList = new List<Deal_Offer__c> ();
            dofList = dealoffrMap.get(OppIds);
            dynamicIds.put(OppIds, dofList.size()) ; // Get the no of Deal_Offer__c for a opportunity from database
        }
        
        for (Deal_Offer__c d:trigger.new){        
            d.id__c= dynamicIds.get(d.opportunity__c)+1 ;
            dynamicIds.put(d.opportunity__c, d.id__c ) ;// save the incremented value to the map, so next Deal_Offer__c will get new incremented id in bulk operations
        }
  
  
}