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
asadim2asadim2 

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?

 

Best Answer chosen by Admin (Salesforce Developers) 
gm_sfdc_powerdegm_sfdc_powerde

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

gm_sfdc_powerdegm_sfdc_powerde

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.

This was selected as the best answer
asadim2asadim2

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).