You need to sign in to do that
Don't have an account?
asadim2
Adding item to related list won't work
// ms is an instance of a custom object with Tasks as its children system.debug('**** TASKS BEFORE: ' + ms.tasks.size()); // gives 2 Task to = new Task(whatId = ms.Id, Subject='New Task'); ms.tasks.add(to); system.debug('**** TASKS AFTER: ' + ms.tasks.size()); // still gives 2
Any ideas?
I presume you got this object with list of tasks populated from a query. I think lists from sub-query are read-only. if you are trying to add a new task to the object, the best way to do it is to instantiate a new Task object with parent id set to custom object record and insert it directly.
All Answers
I presume you got this object with list of tasks populated from a query. I think lists from sub-query are read-only. if you are trying to add a new task to the object, the best way to do it is to instantiate a new Task object with parent id set to custom object record and insert it directly.
Yes, the object instance ms was populated using a query. But if the Tasks related list is read-only then I would've expected getting an exception of some sort. The above code compiles, and executes with no issues. So this looks like an enhancement to me (i.e. the code needs to throw an exception).