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
ethan huntethan hunt 

object instatiation exception


Hi ,

I am getting Avoid instantiating new objects inside loops in the below line. Can this be avoided.Please suggext

Map<Id, Opportunity> mapOpportunityOwnerId;
            List<OpportunityTeamMember> listOpportunityTeamMember;
            if(setOpportunityId != null) {
                // get the map of Opportunity Id and it's corresponding owner Id
                mapOpportunityOwnerId = queryDMLHelper.getOpportunity(setOpportunityId);    
                // get the list of Opportunity team members for a set of Opportunities     
                listOpportunityTeamMember = queryDMLHelper.getListOpportunityTeam(setOpportunityId);
            }
            Map<Id, Set<Id>> mapOpportunitySalesTeamId = new Map<Id, Set<Id>>();       
            if(listOpportunityTeamMember != null) {
            for(OpportunityTeamMember salesTeamMember : listOpportunityTeamMember) {
                setSalesTeamId = new Set<Id>(); ///////////////////////////////////Avoid instantiating new objects inside loops
                if(mapOpportunitySalesTeamId.get((Id)salesTeamMember.get('OpportunityId'))== null) {           
                    setSalesTeamId.add((Id)salesTeamMember.get('UserId'));
                    mapOpportunitySalesTeamId.put((Id)salesTeamMember.get('OpportunityId'), setSalesTeamId);
                }
                else {
                    mapOpportunitySalesTeamId.get((Id)salesTeamMember.get('OpportunityId')).add((Id)salesTeamMember.get('UserId'));
                }
            }
            }
Satish_SFDCSatish_SFDC
You just have to put the same line out of the loop.
I see that you are instatiating a new Set in every iteration of the loop. This simply nulls out the previous one and creates a new object.

Regards,
Satish Kumar
ethan huntethan hunt
Thanks Satish,

Another samll query . I have used listOpportunityTeamMember list in the above code. Do I need to clear the list at the end of the method.
Does it affect the heap size or memory allocation.

Regards
Satish_SFDCSatish_SFDC
If processing on the List is done, then you can clear out the list. This will definitely help clear out the heap space for further code in the same execution context.
But make sure you really do not need this data anymore in the list.

Regards,
Satish Kumar
ethan huntethan hunt
Hi Satish,

Could you please have a look into the below code. Is is necessary to return null. Can we remove the picece of line.

@TestVisible private set<integer> monthsToAdd(date startingDate ,date endingDate ) {
        if(startingDate != null && endingDate != null) {
            for(monthIndex=startingDate.month()  ; monthIndex<= endingDate.month() ; monthIndex++){
                monthsSet.add(monthIndex);
            }   
            return monthsSet;
        }else {
            return null;
        }
    }
Satish_SFDCSatish_SFDC
You cannot just remove the return null statement. The method is actually expected to return a Set<Integer>. So you can either return this or a null.

Regards,
Satish Kumar