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
srikanth j 24srikanth j 24 

Salesforce Roll-up Summary

Hi guys,
Roll up summary using trigger Between Account and Opportunity . i want Opportunity count on Account object  can anyone give me a solution for this if possible give me a code.
Advance Thanks.
 
sfdcMonkey.comsfdcMonkey.com
hi srikanth 

Why are you creating an Apex trigger for this particular requirement.? You just need to create a Roll Up Summary field on Account "Total Number of Opportunities". You can achieve it by Roll Up Summary field.

Log in into salesforce.com -> User Name -> Setup -> App Setup -> Customize -> Account -> Fields -> New -> Roll Up Summary
User-added image

its solve 
Thanks 
mark it solve if it helps you :)
srikanth j 24srikanth j 24
i know bro i can use rollup summary but i got 1 requirement through trigger they are asking that's why i am asking
sfdcMonkey.comsfdcMonkey.com
hi srikanth 
try this trigger code
 
trigger opportunityCount on Opportunity (after insert, after update, after delete, after undelete) {
    Map<Id, List<Opportunity >> mapAcctIdContactList = new Map<Id, List<Opportunity >>();
    Map<Id, List<Opportunity >> mapAcctIdDelContactList = new Map<Id, List<Opportunity >>();
    Set<Id> AcctIds = new Set<Id>();    
    List<Account> listAcct = new List<Account>();
    
    if(trigger.isInsert) {
        for(Opportunity Con : trigger.New) {
            if(String.isNotBlank(Con.AccountId)) {
                if(!mapAcctIdContactList.containsKey(Con.AccountId)) {
                    mapAcctIdContactList.put(Con.AccountId, new List<Opportunity >());
                }
                mapAcctIdContactList.get(Con.AccountId).add(Con); 
                AcctIds.add(Con.AccountId);
            }   
        }  
    }
    
    if(trigger.isUpdate) {
        for(Opportunity Con : trigger.New) {
            if(String.isNotBlank(Con.AccountId) && Con.AccountId != trigger.oldMap.get(Con.Id).AccountId) {
                if(!mapAcctIdContactList.containsKey(Con.AccountId)){
                    mapAcctIdContactList.put(Con.AccountId, new List<Opportunity >());
                }
                mapAcctIdContactList.get(Con.AccountId).add(Con); 
                AcctIds.add(Con.AccountId);
            } else if(String.isBlank(Con.AccountId) && String.isNotBlank(trigger.oldMap.get(Con.Id).AccountId)) {
                if(!mapAcctIdDelContactList.containsKey(Con.AccountId)){
                    mapAcctIdDelContactList.put(Con.AccountId, new List<Opportunity >());
                }
                mapAcctIdDelContactList.get(Con.AccountId).add(Con);   
                AcctIds.add(trigger.oldMap.get(Con.Id).AccountId);
            }
        }  
    }
    
    if(trigger.isUndelete) {
        for(Opportunity Con : trigger.new) {
            if(String.isNotBlank(Con.AccountId)){
                if(!mapAcctIdContactList.containsKey(Con.AccountId)){
                    mapAcctIdContactList.put(Con.AccountId, new List<Opportunity >());
                }
                mapAcctIdContactList.get(Con.AccountId).add(Con);     
                AcctIds.add(Con.AccountId);
            }
        }  
    }      

    if(trigger.isDelete) {
        for(Opportunity Con : trigger.Old) {
            if(String.isNotBlank(Con.AccountId)){
                if(!mapAcctIdDelContactList.containsKey(Con.AccountId)){
                    mapAcctIdDelContactList.put(Con.AccountId, new List<Opportunity >());
                }
                mapAcctIdDelContactList.get(Con.AccountId).add(Con);    
                AcctIds.add(Con.AccountId); 
            }
        }  
    }   
    
    if(AcctIds.size() > 0) {
        listAcct = [SELECT Id, Number_of_Opportunity__c FROM Account WHERE Id IN : AcctIds];
        
        for(Account acct : listAcct) {
            Integer noOfopps = 0;
            if(mapAcctIdContactList.containsKey(acct.Id)) {
                noOfopps += mapAcctIdContactList.get(acct.Id).size();
            }
            if(mapAcctIdDelContactList.containsKey(acct.Id)) {
                noOfopps -= mapAcctIdDelContactList.get(acct.Id).size();
            }
            acct.Number_of_Opportunity__c = acct.Number_of_Opportunity__c == null ? noOfopps : (acct.Number_of_Opportunity__c + noOfopps);
        }
        
        update listAcct;    
    }
}

and make sure  Number_of_Opportunity__c field create in your account object :)
than create a new account and add a new opportunity with this account and save. after save Number_of_Opportunity__c  count tha related opportunity 
Thanks 
Mark it best answer if it helps you:)