You need to sign in to do that
Don't have an account?
anonymousXYZ
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'
Actual 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=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})
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 :
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})
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
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
Is it a expected behaviour where cUser is getting changed which is yet not updated?
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