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
dharmik thummardharmik thummar 

I want Soql outside of for loop.

I want [select id,Amount from Opportunity where Fund__c =: funRec.Id]  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);
            }

 
RKSalesforceRKSalesforce
Hi Dharmik,

Please find below code to avoid query inside for loop:
Map<Id, Opportunity> FundIdAndOpportunityMap = New Map<Id, Opportunity>(); 
for(Opportunity opp :[select id,Amount,Fund__c from Opportunity where Fund__c =: fundIds]){
	FundIdAndOpportunityMap.Put(opp.Fund__c.Id, opp);
}
for(Fund__c funRec : [select id from Fund__c where id in :fundIds])
{
    oppMap.put(funRec.Id, FundIdAndOpportunityMap.get(funRec.Id));
    fundMap.put(funRec.Id, funRec);
}
Please let mark as best answer if helped. Please let me know incase you need any further assistance.

Regards,
Ramakant
 
dharmik thummardharmik thummar
A non foreign key field cannot be referenced in a path expression: Fund__c on line 19. 
and Method does not exist or incorrect signature: void put(Id, Opportunity) from the type Map<Id,List<Opportunity>> is the error now.

My complete code is like 
public class OpportunityCustomRollup {
   public static void CountRollup(list<Opportunity> OpportunityList)
    {
        Set<Id> fundIds = new Set<Id>();
        id objrecordtypeid = [SELECT Id FROM RecordType WHERE DeveloperName ='Fund_Raising'].Id;
        Map<Id,list<Opportunity>> oppMap = new Map<Id,list<Opportunity>>();
        Map<Id,Fund__c> fundMap = new Map<Id,Fund__c>();
        Set<Id> setfundrec = new Set<Id>();
        
        
        for(Opportunity oppVar : OpportunityList)
        {
            fundIds.add(oppVar.fund__c);
        }


         Map<Id, Opportunity> FundIdAndOpportunityMap = New Map<Id, Opportunity>(); 
            for(Opportunity opp :[select id,Amount,Fund__c from Opportunity where Fund__c =: fundIds]){
                FundIdAndOpportunityMap.Put(opp.Fund__c.Id, opp);
            }
        
        for(Fund__c funRec : [select id from Fund__c where id in :fundIds])
        {
            oppMap.put(funRec.Id, FundIdAndOpportunityMap.get(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.Total_opportunity_amount__c = Amount;
            listOfFundRecsToUpdate.add(fundRec);
        }
        update listOfFundRecsToUpdate;
    }
}

 
RKSalesforceRKSalesforce
Please try this code:
public class OpportunityCustomRollup {
   public static void CountRollup(list<Opportunity> OpportunityList)
    {
        Set<Id> fundIds = new Set<Id>();
        id objrecordtypeid = [SELECT Id FROM RecordType WHERE DeveloperName ='Fund_Raising'].Id;
        Map<Id,list<Opportunity>> oppMap = new Map<Id,list<Opportunity>>();
        Map<Id,Fund__c> fundMap = new Map<Id,Fund__c>();
        Set<Id> setfundrec = new Set<Id>();
               
        for(Opportunity oppVar : OpportunityList)
        {
            fundIds.add(oppVar.fund__c);
        }

        Map<Id, Opportunity> FundIdAndOpportunityMap = New Map<Id, Opportunity>(); 
            for(Opportunity opp :[select id,Amount,Fund__c from Opportunity where Fund__c =: fundIds]){
				FundIdAndOpportunityMap.Put(opp.Fund__c, opp);
            }
        
        for(Fund__c funRec : [select id from Fund__c where id in :fundIds])
        {
            oppMap.put(funRec.Id, FundIdAndOpportunityMap.get(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.Total_opportunity_amount__c = Amount;
            listOfFundRecsToUpdate.add(fundRec);
        }
        update listOfFundRecsToUpdate;
    }
}

Hope this will work.

Regards,
Ramakant​
 
dharmik thummardharmik thummar
Method does not exist or incorrect signature: void put(Id, Opportunity) from the type Map<Id,List<Opportunity>> on line 22
RKSalesforceRKSalesforce
Hello,
Please try below code:
public class OpportunityCustomRollup {
   public static void CountRollup(list<Opportunity> OpportunityList)
    {
        Set<Id> fundIds = new Set<Id>();
        id objrecordtypeid = [SELECT Id FROM RecordType WHERE DeveloperName ='Fund_Raising'].Id;
        Map<Id,list<Opportunity>> oppMap = new Map<Id,list<Opportunity>>();
        Map<Id,Fund__c> fundMap = new Map<Id,Fund__c>();
        Set<Id> setfundrec = new Set<Id>();
               
        for(Opportunity oppVar : OpportunityList)
        {
            fundIds.add(oppVar.fund__c);
        }

        Map<Fund__c, Opportunity> FundIdAndOpportunityMap = New Map<Fund__c, Opportunity>(); 
            for(Opportunity opp :[select id,Amount,Fund__c from Opportunity where Fund__c =: fundIds]){
				FundIdAndOpportunityMap.Put(opp.Fund__c, opp);
            }
        
        for(Fund__c funRec : [select id from Fund__c where id in :fundIds])
        {
            oppMap.put(funRec, FundIdAndOpportunityMap.get(funRec));
            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.Total_opportunity_amount__c = Amount;
            listOfFundRecsToUpdate.add(fundRec);
        }
        update listOfFundRecsToUpdate;
    }
}