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
anonymousXYZanonymousXYZ 

list variable is not working as expected in for loop

While iteration through a list in for loop, I noticed value is changed for the list without updating it. In the below code list 'cUser' should store same value for any iteration but here it is changing when a 'for' local variable is updated with new value, and same change is reflecting in the List 'listContact'

Please help me to understand what I am doing wrong.

Scenario :
Insert a new contact with the below details(contact standard fields) and run the below mentioned code in 'Execute Anonymous Window'
fistname='fname'
lastname='LastNameTest'
 
String str='LastNameTest';
List <Contact> listContact = new List <Contact>();
List<Contact> cUser = [Select id, firstname,lastname from Contact where lastname = :str];
for(integer i=0;i<2;i++){
    system.debug('** i='+i+'  ** cUser **  ' + cUser);
	for(Contact conTemp : cUser){
        if(i==0){
            system.debug('** if- before **' + conTemp);
            conTemp.firstname = 'abc';
            listContact.add(conTemp);
            system.debug('** if - after **' + conTemp);
        }
        else if(i==1){ 
            system.debug('## else-if-before ##' + conTemp);
            conTemp.firstname = 'xyz';
            listContact.add(conTemp);
            system.debug('## else-if-after ##' + conTemp);
        } 
     }
	system.debug('listContact = '+listContact);
}
                            

Actual Output : 

User-added image

01:30:10:009 USER_DEBUG [5]|DEBUG|** i=0  ** cUser **  (Contact:{Id=0030I00001q6J8QQAU, FirstName=fName, LastName=LastNameTest})
01:30:10:009 USER_DEBUG [8]|DEBUG|** if- before **Contact:{Id=0030I00001q6J8QQAU, FirstName=fName, LastName=LastNameTest}
01:30:10:009 USER_DEBUG [11]|DEBUG|** if - after **Contact:{Id=0030I00001q6J8QQAU, FirstName=abc, LastName=LastNameTest}
01:30:10:009 USER_DEBUG [20]|DEBUG|listContact = (Contact:{Id=0030I00001q6J8QQAU, FirstName=abc, LastName=LastNameTest})
01:30:10:009 USER_DEBUG [5]|DEBUG|** i=1  ** cUser **  (Contact:{Id=0030I00001q6J8QQAU, FirstName=abc, LastName=LastNameTest})
01:30:10:009 USER_DEBUG [14]|DEBUG|## else-if-before ##Contact:{Id=0030I00001q6J8QQAU, FirstName=abc, LastName=LastNameTest}
01:30:10:010 USER_DEBUG [17]|DEBUG|## else-if-after ##Contact:{Id=0030I00001q6J8QQAU, FirstName=xyz, LastName=LastNameTest}
01:30:10:010 USER_DEBUG [20]|DEBUG|listContact = (Contact:{Id=0030I00001q6J8QQAU, FirstName=xyz, LastName=LastNameTest}, Contact:{Id=0030I00001q6J8QQAU, FirstName=xyz, LastName=LastNameTest})

Expected output::
01:30:10:009 USER_DEBUG [5]|DEBUG|** i=0  ** cUser **  (Contact:{Id=0030I00001q6J8QQAU, FirstName=fName, LastName=LastNameTest})
01:30:10:009 USER_DEBUG [8]|DEBUG|** if- before **Contact:{Id=0030I00001q6J8QQAU, FirstName=fName, LastName=LastNameTest}
01:30:10:009 USER_DEBUG [11]|DEBUG|** if - after **Contact:{Id=0030I00001q6J8QQAU, FirstName=abc, LastName=LastNameTest}
01:30:10:009 USER_DEBUG [20]|DEBUG|listContact = (Contact:{Id=0030I00001q6J8QQAU, FirstName=abc, LastName=LastNameTest})
01:30:10:009 USER_DEBUG [5]|DEBUG|** i=1  ** cUser **  (Contact:{Id=0030I00001q6J8QQAU, FirstName=fName, LastName=LastNameTest})
01:30:10:009 USER_DEBUG [14]|DEBUG|## else-if-before ##Contact:{Id=0030I00001q6J8QQAU, FirstName=fName, LastName=LastNameTest}
01:30:10:010 USER_DEBUG [17]|DEBUG|## else-if-after ##Contact:{Id=0030I00001q6J8QQAU, FirstName=xyz, LastName=LastNameTest}
01:30:10:010 USER_DEBUG [20]|DEBUG|listContact = (Contact:{Id=0030I00001q6J8QQAU, FirstName=abc, LastName=LastNameTest}, Contact:{Id=0030I00001q6J8QQAU, FirstName=xyz, LastName=LastNameTest})
Best Answer chosen by anonymousXYZ
Deepali KulshresthaDeepali Kulshrestha
Hi,

In your code, if you will try to update the same object in every iteration it will let in the occurrence of a problem that will reflect changes in all the instances that you are adding every time in the list.
You can see this in your code output:

01:30:10:010 USER_DEBUG [20]|DEBUG|listContact = (Contact:{Id=0030I00001q6J8QQAU, FirstName=xyz, LastName=LastNameTest}, Contact:{Id=0030I00001q6J8QQAU, FirstName=xyz, LastName=LastNameTest})

Thanks.
Deepali Kulshrestha

All Answers

Deepali KulshresthaDeepali Kulshrestha
Hi Abhishek,
Please make these changes in your code to achieve the expected output:

String str='LastNameTest';
List <Contact> listContact = new List <Contact>();
List<Contact> cUser = [Select id, firstname,lastname from Contact where lastname = :str];
for(integer i=0;i<2;i++){
  
    system.debug('** i='+i+'  ** cUser **  ' + cUser);
    for(Contact conTemp : cUser){
        if(i==0){
            Contact con=new Contact();
            con.firstName='abc';
            system.debug('** if- before **' + conTemp);
           // conTemp.firstname = 'abc';
            listContact.add(con);
            system.debug('** if - after **' + con);
        }
        else if(i==1){ 
               system.debug('## else-if-before ##' + conTemp);
             Contact con=new Contact();
            con.firstname = 'xyz';
         
             
            listContact.add(con);
            system.debug('## else-if-after ##' + con);
        } 
     }
    system.debug('listContact = '+listContact);
}

Please mark it as best answer if it helps you.

Thanks.
Deepali Kulshrestha
anonymousXYZanonymousXYZ
Thanks Deepali for checking this, I can se here you create one more variable (Contact con=new Contact();) through which you are assigning value, but my requirement is to update the same contact from list cUser and in realtime Contacts are having large number of fields so creating a new variable and changing a filed will not help here.

Is it a expected behaviour where cUser is getting changed which is yet not updated?
Deepali KulshresthaDeepali Kulshrestha
Hi,

In your code, if you will try to update the same object in every iteration it will let in the occurrence of a problem that will reflect changes in all the instances that you are adding every time in the list.
You can see this in your code output:

01:30:10:010 USER_DEBUG [20]|DEBUG|listContact = (Contact:{Id=0030I00001q6J8QQAU, FirstName=xyz, LastName=LastNameTest}, Contact:{Id=0030I00001q6J8QQAU, FirstName=xyz, LastName=LastNameTest})

Thanks.
Deepali Kulshrestha
This was selected as the best answer
anonymousXYZanonymousXYZ
is should you deepclose() to use a different referance.