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
Pruthvi Mada 5Pruthvi Mada 5 

adding a new record to List<account.id, List<asset>>

I want to add a new asset record to an existing List<Id, List<asset>>. The code doesn't throw any compile error or runtime error and the asset is not being added to the list. can someone please help ?

public class testList {
    
    public void testing()
    {
    List<Account> accountlst = [select id, (select id, name from assets) from account where account.id = 'xxxxxxxxxxxx'];
    system.debug('orig list'+ accountlst);
         system.debug('orig list'+ accountlst[0].assets);
    List<asset> bb = [select id, name, Asset_ID__c from asset where account.id = 'xxxxxxxxxxx' limit 2];
    accountlst[0].assets.add(bb[1]);
    system.debug('updated list'+accountlst);
        system.debug('updated list'+accountlst[0].assets);
    }    

}
Raj VakatiRaj Vakati
Its wokring for me  try this code
 
public class testList {

public void testing()
{
List<Account> accountlst = [select id, (select id, name from assets) from account where account.id = 'xxxxxxxxxxxx'];
system.debug('orig list'+ accountlst);
	 system.debug('orig list'+ accountlst[0].assets);
List<asset> bb = [select id, name, Asset_ID__c from asset where account.id = 'xxxxxxxxxxx' limit 2];
if(bb[1]!=null){
accountlst[0].assets.add(bb[1]);
system.debug('updated list'+accountlst);
	system.debug('updated list'+accountlst[0].assets);
}
}    

}



 
Pruthvi Mada 5Pruthvi Mada 5
I tried adding the if condition. Did not help. 
Pruthvi Mada 5Pruthvi Mada 5
Raj,

Looks like this is not possible. check this link:
https://salesforce.stackexchange.com/questions/7731/is-a-list-from-a-subquery-modifiable

Anyways, thanks for looking into this.