You need to sign in to do that
Don't have an account?
Kishore Dasari
How to use addAll() method in For loop?
I want to use addAll() instead of add() in below code. How to achieve this?
//one list is there: myList1
List<String> myList1 = New List<String>;
//I want to add Name field values from my Custom Meta Data to myList
for(myCustomMetaData__mdt mtdVar: [select id, Name from myCustomMetaData__mdt LIMIT 1000]){
myList1.add(mtdVar.Name);
}
//one list is there: myList1
List<String> myList1 = New List<String>;
//I want to add Name field values from my Custom Meta Data to myList
for(myCustomMetaData__mdt mtdVar: [select id, Name from myCustomMetaData__mdt LIMIT 1000]){
myList1.add(mtdVar.Name);
}
Greetings to you!
add() is used in List when you wish to add ONE element to the end of the list.
addAll() is used when you wish to add contents of an already existing List to a List. Both lists must be of the same type.
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.
Thanks and Regards,
Khan Anas
You can do as below:
List<String> myList1 = New List<String>;
List<String> myList2 = New List<String>;
for(myCustomMetaData__mdt mtdVar: [select id, Name from myCustomMetaData__mdt LIMIT 1000]){
myList1.add(mtdVar.Name);
}
myList2 .addAll(myList1); //Adding list to list