Skip to main content

Memory Reference in Apex

Recently during one of project, I was creating a map of integer and list of integer. My code snippet something like:-


I was expecting something like {1=(1), 2=(1,2 ), 3=(1,2,3), 4=(1,2,3,4), 5=(1,2,3,4,5), 6=(1,2,3,4,5,6), ...} as output. However, I was surprise to see the output as {1=(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), 2=(already output), 3=(already output), 4=(already output), 5=(already output), ...}.

I was wondering what's wrong with my code and started exploring how I can fix it.

After going through blogs & docs, I get to know that if I call some method like clear() on a list and put it in a map and use the same variable with modified values for next element of map, ultimately SFDC stores address of same variable in each element of map, because of which I finally get same value in each element of map as per above code.

If I make a small change in my code,as shown below, my code will start yielding expected result:-


If you closely look into change, instead of using clear() method, in each iteration, I have used 'new List<integer>()', which make sure that SFDC assigns different addresses in each iteration and I am able to get expected value.


Comments

  1. It was worth reading this blog. Quite intersting one. :)

    ReplyDelete
  2. Thanks for the article Digamber. Lead me in the right direction to solve a problem.

    ReplyDelete

Post a Comment

Popular posts from this blog

Mashup Integration in Salesforce

During preparation for TA certification exam, I came across a word Mashup for integration a number of times. I explored about it and below is description:- Mashups, sometimes called “composites,” are hybrid applications created by bringing together several data sources and Web services to create a new application or to add value to an existing application. Behind the scenes, mashups may require different levels of integration, depending on whether the mashed-up data is only meant to be viewed, whether it can be edited, and whether data is actually transferred between systems. There are three types of mashup:- Client Presentation Mashup - In this type of mashup the integration takes place strictly at the visual level. It makes possible to view data from two or more applications in a browser,  without actually moving data between the applications. Example - Google Maps. Client Service Mashup - As mashups evolve, they are becoming more complex and sophisticated. Client...

Make Calls to PageReference.getContent() in Asynchronous Apex - Winter16

You can now make calls to the getContent() and getContentAsPdf() methods of the PageReference class from within asynchronous Apex such as Batch Apex, Schedulable and Queueable classes, and @future methods. This allows you to design much more flexible and scalable services that, for example, render Visualforce pages as PDF files.

Grant Access Using Hierarchies

Problem There is a custom object say 'XYZ' and OWD for this is set to ' Private ', which means record of this can be seen by only owner and users above in role-hierarchy and territory. However, to share this with other user, we can manually share it. The problem is that I don't want other users, who are above in role-hierarchy and territory of the user with whom record has shared, can see it. Solution We can un-check ' Grant Access Using Hierarchies ' check box for object 'XYZ' on 'Sharing Settings' page. We can go to Setup >> Security Controls >> Sharing Settings and click on ' Edit ' button. On the edit page, we can un-check ' Grant Access Using Hierarchies ' for required object.  Major uses of 'Grant Access Using Hierarchies' are:- If you disable the Grant Access Using Hierarchies option, sharing with a role or territory and subordinates only shares with the users directly asso...