+ Start a Discussion
dharmik thummardharmik thummar 

I've created trigger for below mentioned scenario and I want solution for that.

There is two object: Fund and opportunity. and opportunity object has lookup of Fund. I've devloped one trigger that sum up the total amount from all the relative opportunity and set that amount in custom field of fund object. now problem in my code is that whenever I try to create bulk opportunity using CSV file that contain data for multiple fund at that time it sum up the total of all fund and set that only in first record fund ID. I need solution using Map.
Thannk you.

Trigger:
trigger newLeadTrigger on Opportunity (after insert , after update, after delete , after undelete) {
    
    if(trigger.isAfter && (trigger.isInsert || trigger.isUpdate || trigger.isUndelete)){
           OpportunityCustomRollup.CountRollup(Trigger.new);
    }
    
    if(Trigger.isDelete)
    {
        OpportunityCustomRollup.CountRollup(Trigger.old);
    }
}


Controller class:

public class OpportunityCustomRollup {
   
    public static void CountRollup(List<Opportunity> lstOpportunity){
        
        set<id> oppIds = new set<id>();
        map<string, integer> classroomIDToDeskCountMap = new map<string, integer>();
        id objrecordtypeid = [SELECT Id FROM RecordType WHERE DeveloperName ='Fund_Raising'].Id;
        double amount = 0;
        
        try {
                for (Opportunity objOpportunity : lstOpportunity){
                    oppIds.add(objOpportunity.Fund__c);
                }
            
                Fund__c objfund = [SELECT Id, Total_opportunity_amount__c  FROM Fund__c WHERE Id = :oppIds];
                List<Opportunity> list_Opportunity = [SELECT Id, Amount FROM Opportunity WHERE Fund__c = :objfund.Id and StageName = 'Closed Won' and RecordTypeId =: objrecordtypeid];
            
            
                 for(Opportunity AmountOpportunity : list_Opportunity) {
                        amount += AmountOpportunity.amount; 
                 }
            
            
                  objfund.Total_opportunity_amount__c = amount;
                  update objfund;   
            } 
       
        
        catch (Exception e) {
                System.debug(e);
            }
        
    }

}
  
Best Answer chosen by dharmik thummar
Dayakar.DDayakar.D
Hi dharmik thummar, 

Please try below code, it will work.
 
//Trigger 
trigger OpportunityTigger on Opportunity (after insert, after Update, after delete) {
    if(trigger.isInsert)
    {
         OpportunityCustomRollup.testing(trigger.new);
    }
    if(trigger.isDelete)
    {
        OpportunityCustomRollup.testing(trigger.old);
    }
}

//Helper

public class OpportunityCustomRollup {
    
    public static void testing(list<Opportunity> OpportunityList)
    {
        Set<Id> fundIds = new Set<Id>();
        Map<Id,list<Opportunity>> oppMap = new Map<Id,list<Opportunity>>();
        Map<Id,Fund__c> fundMap = new Map<Id,Fund__c>();
        for(Opportunity oppVar : OpportunityList)
        {
            fundIds.add(oppVar.fund__c);
        }
        for(Fund__c funRec : [select id from Fund__c where id in :fundIds])
        {
            oppMap.put(funRec.Id, [select id,Amount from Opportunity where Fund__c =: funRec.Id]);
            fundMap.put(funRec.Id, funRec);
        }
        list<Fund__c> listOfFundRecsToUpdate = new list<Fund__c>();
        for(String Id : oppMap.keySet())
        {
            Decimal Amount =0;
            for(Opportunity oppRec : oppMap.get(Id))
            {
                Amount = Amount + oppRec.Amount;
            }
            Fund__c fundRec = fundMap.get(Id);
            fundRec.Anilji__Total_opportunity_amount__c =Amount;
            listOfFundRecsToUpdate.add(fundRec);
        }
        update listOfFundRecsToUpdate;
    }
    
}

BestRegards,
Dayakar.D

All Answers

Dayakar.DDayakar.D
Hi dharmik thummar, 

Please try below code, it will work.
 
//Trigger 
trigger OpportunityTigger on Opportunity (after insert, after Update, after delete) {
    if(trigger.isInsert)
    {
         OpportunityCustomRollup.testing(trigger.new);
    }
    if(trigger.isDelete)
    {
        OpportunityCustomRollup.testing(trigger.old);
    }
}

//Helper

public class OpportunityCustomRollup {
    
    public static void testing(list<Opportunity> OpportunityList)
    {
        Set<Id> fundIds = new Set<Id>();
        Map<Id,list<Opportunity>> oppMap = new Map<Id,list<Opportunity>>();
        Map<Id,Fund__c> fundMap = new Map<Id,Fund__c>();
        for(Opportunity oppVar : OpportunityList)
        {
            fundIds.add(oppVar.fund__c);
        }
        for(Fund__c funRec : [select id from Fund__c where id in :fundIds])
        {
            oppMap.put(funRec.Id, [select id,Amount from Opportunity where Fund__c =: funRec.Id]);
            fundMap.put(funRec.Id, funRec);
        }
        list<Fund__c> listOfFundRecsToUpdate = new list<Fund__c>();
        for(String Id : oppMap.keySet())
        {
            Decimal Amount =0;
            for(Opportunity oppRec : oppMap.get(Id))
            {
                Amount = Amount + oppRec.Amount;
            }
            Fund__c fundRec = fundMap.get(Id);
            fundRec.Anilji__Total_opportunity_amount__c =Amount;
            listOfFundRecsToUpdate.add(fundRec);
        }
        update listOfFundRecsToUpdate;
    }
    
}

BestRegards,
Dayakar.D
This was selected as the best answer
dharmik thummardharmik thummar
Thanks Dayakar.D but I also want SOQL outside of for loop.
for(Fund__c funRec : [select id from Fund__c where id in :fundIds])
            {
                oppMap.put(funRec.Id, [select id,Amount from Opportunity where Fund__c =: funRec.Id]);
                fundMap.put(funRec.Id, funRec);
            }