0

Below Query returns parent_Id_c and Id from child record, even though fields wasn't listed in the query.How to avoid system from returning parentId or is it possible to remove parentId field reference from the child record.

obja = [select id,(select name__c from child__r) from parent where id = parent.ID]

objb = a.child__r

when objb printed in debug log the result as follows.

child__c:{parent_Id__c=a1DM000003ND2C, Id= a1CM0000003Kg8nM, Name__c=test}
6
  • 2
    You always get Id, it is impossible to remove from query results. The same is apparently true for child sub-queries.
    – Adrian Larson
    Nov 6, 2015 at 19:30
  • 1
    @AdrianLarson you're absolutely correct. Id is always returned. I can't find the documentation on this, but I know it's been this way since approximately forever. You should write up an answer for it. Also, SOSL always returns ID values, too.
    – sfdcfox
    Nov 6, 2015 at 19:32
  • @sfdcfox i can only remove id by cloning child record to a different object but it is still retaining parentId from the cloned child record.
    – vraavi
    Nov 6, 2015 at 19:42
  • 1
    whats ur actual use case & why do you want to exclude those fields ? Nov 6, 2015 at 19:49
  • @VamsiKrishna I am trying to insert both parent and child records in single DML Statement using external id of parent, when any changes happen to the values in existing parent or child records.
    – vraavi
    Nov 6, 2015 at 20:08

1 Answer 1

0

You always get Id, it is impossible to remove from query results. The same is apparently true for child sub-queries.

You could theoretically remove it yourself through post-processing.

Parent__c parent = [
    SELECT (Select Name__c FROM Children__r)
    FROM Parent__c WHERE Id = :parentId
];
system.assertNotEquals(null, parent.Id);
for (Child__c child : parent.Children__r)
{
    system.assertNotEquals(null, child.Id);
    system.assertNotEquals(null, child.Parent__c);
    child.Id = null;
    child.Parent__c = null;
    system.assertEquals(null, child.Id);
    system.assertEquals(null, child.Parent__c);
}

Note that the above will cause updates to fail, because you are clearing out the Id value. Your question does not make clear why you do not want these values, so it is hard to recommend a best course of action.

5
  • Actually i want to remove remove the parentID field reference from child object but not just value of the parentId. scenario is that when ever any changes happen to the values in existing parent or child records system has to insert both parent and child records at same time using single DML statement.
    – vraavi
    Nov 6, 2015 at 21:16
  • 1
    @vraavi That is completely impossible.
    – Adrian Larson
    Nov 6, 2015 at 21:34
  • is there any better solution to handle this scenario rather than inserting parent and child separately. There are bunch of child records associated to parent so I don't like to iterate all child records to assign parentId before insert.
    – vraavi
    Nov 6, 2015 at 22:42
  • @vraavi It is extremely unclear what you are trying to accomplish through this functionality. Please clarify the underlying use case. What is your desired business outcome?
    – Adrian Larson
    Nov 6, 2015 at 22:46
  • @vraavi - while APEX can manage object relationships in memory, SFDC DML can only operate on a single sObject type per call.
    – cropredy
    Nov 7, 2015 at 0:34

You must log in to answer this question.

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