You need to sign in to do that
Don't have an account?
Vaibhav Sule
Updating field for a list of custom objects
Hello,
Is it possible in Apex to update a particular field in a custom object for a list of those objects at the same time?
Here's what I am trying to do:
I have a custom object 'myCustomObj', which has a custom field 'myCustomField'.
I have a list of these objects created already. I want to update the 'myCustomField' for all these objects.
Here's a snippet of my code, which is creating the list of objects correctly, but the update function doesn't seem to work.
myCustomObj[] myList = new List<myCustomObj>();
myList = [Select Name, ID, myCustomField from myCustomObj]; //this list gets created as expected
integer max = mylist.size();
for (integer i = 0; i< max; i++){
Decimal x = myList[i].myCustomField; //I am able to read the value in this custom field correctly for all objects in the list
myList[i].myCustomField = x + 1;
update myList[i];
}
update myList;//this seems to just update the list. How do I actually update the custom object?
Thanks in advance
Is it possible in Apex to update a particular field in a custom object for a list of those objects at the same time?
Here's what I am trying to do:
I have a custom object 'myCustomObj', which has a custom field 'myCustomField'.
I have a list of these objects created already. I want to update the 'myCustomField' for all these objects.
Here's a snippet of my code, which is creating the list of objects correctly, but the update function doesn't seem to work.
myCustomObj[] myList = new List<myCustomObj>();
myList = [Select Name, ID, myCustomField from myCustomObj]; //this list gets created as expected
integer max = mylist.size();
for (integer i = 0; i< max; i++){
Decimal x = myList[i].myCustomField; //I am able to read the value in this custom field correctly for all objects in the list
myList[i].myCustomField = x + 1;
update myList[i];
}
update myList;//this seems to just update the list. How do I actually update the custom object?
Thanks in advance
List<myCustomObj> myList = new List<myCustomObj>();
myList = [Select Name, ID, myCustomField from myCustomObj]; //this list gets created as expected
for(myCustomObj obj : myList )
{
obj.myCustomField = obj.myCustomField + 1;
}
if(myList.size() > 0)
{
update myList;//this seems to just update the list. How do I actually update the custom object?
}
NOTE:- Never use DML inside the for loop.
Please let us know if this will help you
All Answers
List<myCustomObj> myList = new List<myCustomObj>();
myList = [Select Name, ID, myCustomField from myCustomObj]; //this list gets created as expected
for(myCustomObj obj : myList )
{
obj.myCustomField = obj.myCustomField + 1;
}
if(myList.size() > 0)
{
update myList;//this seems to just update the list. How do I actually update the custom object?
}
NOTE:- Never use DML inside the for loop.
Please let us know if this will help you