3

I queried a the parent object and its Kid(Kid__C). Assigned it to a list of Parent object:

List<Parent__c> p = [SELECT id, name (SELECT id, name FROM kids__r order by CreatedDate DESC),
            FROM Parent__c WHERE id IN: AlistWithParentId];

// After qurying the above list , I check which parent does not have children using p.kids__r.size(); and create a kid for them. After I do this I want to add the kid's id to the p.kids__r list of each element in List<Parent__c> p. Basically I do not want to make another SOQL to fetch the parent__c records from database instead use the already queried List<Parent__c> p to get the kids.

I tried an approach where I created a Map with parent id and with list of ids of the kid but I am unable to add it to the p.kids__r list.

For(Parent pobj : p){

// loop through the Map and get the list of
// RMA orders and add it to pobj.kids__r.add();

pobj.kids__r.add(kid__c);
system.debug(ponj.kids__r.size()); // this gives the size as 0, that means the above line of code did not add the kid to the list.
pobj.Name='hello';
system.debug(pobj.Name); // this gave me the name as hello. Why was this variable updated but above list could not be?

}
3
  • Possible duplicate: Reading and writing child relationships of not-yet persisted SObjects?
    – Adrian Larson
    Commented May 4, 2017 at 22:08
  • I have the kid__c records inserted. Now I want to connect them to the parents using parent__c.kids__r.add() . WHich will have list of all the kids of that parent. The other link does not explain how I can achieve it? Does it make sense. Any explanation will be helpful on how I can achieve this
    – SfdcBat
    Commented May 4, 2017 at 22:12
  • 1
    Pretty much you have to re-query. If you want to stay in memory the only workaround I was able to find (perhaps others will chime in) is a serialization technique.
    – Adrian Larson
    Commented May 4, 2017 at 22:13

2 Answers 2

4

I believe your only option is to re-query. Unless you want to do some crazy serialization hijinks like I laid out in this other post. Basically, you would probably have to do something like the following:

class Proxy
{
    final List<Child__c> records;
    Proxy(List<Child__c> children) { records = children; }
    Integer totalSize { get { return records.size(); } }
    Boolean done = true;
}

// elsewhere...
Map<String, Object> data = parent.getPopulatedFieldsAsMap();
data.put('ChildRelationshipName__r', new Proxy(allChildren));
parent = (Parent__c)JSON.deserialize(JSON.serialize(data), Parent__c.class);
0
0

I had a similar requirement for a custom object on Account and replaced the old account record in the results map with a newly constructed account.

Account theAccnt = mapResults.get(accntId);
//now, is the Child__c: childToCheck already listed?
Boolean listed = false;
for (Child__c__c theChild : theAccnt.Children__r){
    if (theChild.name == childToCheck.name){
        listed = true;
    }
}//next Child__c

//if not listed, then add it
if (!listed){
    List <Child__c__c> lstChildren = theAccnt.Children__r;
    lstChildren.add(childToCheck);
    //clone the account and add the updated list of Children
    String childPayload = JSON.serialize(new Proxy(lstChildren));
    String payload = '{"Child__cs__r": ' + childPayload + '}';
    Account record = (Account)JSON.deserialize(payload, Account.class);
    //update other fields
    record.Id = theAccnt.Id;
    record.Name = theAccnt.name;
    record.BillingLatitude = theAccnt.BillingLatitude;
    record.BillingLongitude = theAccnt.BillingLongitude;
    record.BillingCity = theAccnt.BillingCity;
    record.BillingStreet = theAccnt.BillingStreet;
    record.Phone = theAccnt.Phone;
    //replace the account  
    mapResults.remove(accntId);
    mapResults.put(record.Id,record);
}//endif not listed

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .