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
Ram_SfdcRam_Sfdc 

The SOQL query parent record from Child object trigger showing no records

Hi,

I have a visualforce page, through which I am creating a Parent and child record (master-detail relationship). Once the child is created, based on some criteria the child record updates the parent. 

In afterInsert of the child object, I have a SOQL query on Parent object and it returns 0 records.
Following is the code that I am using:

Set<Id> setParentIds = new Set<Id>();

for(){
setParentIds.add(child.Parent__c)
}
System.debug(setParentIds); - Works fine and displays Ids of parent Object

List<parent__c> = [Select Id, field__c From parent Where Id IN:setParentIds] - This will get 0 records. 

This is totally weird, I am getting Ids in 'setParentIds' Set and when I Query why would the Query show 0 records? Any thoughts! What am I missing here! 
Aparna Hegde 1Aparna Hegde 1
Hi Ram,

Try this -

trigger Test on Child_Accnt__c (after insert) {
Set<Id> setParentIds = new Set<Id>();

for(child_accnt__c ca:trigger.new){
setParentIds.add(ca.ParAccountId__c);
}
System.debug(setParentIds);

List<Account> Accnts = new List<Account>([Select Id, industry From account Where Id IN:setParentIds]);
system.debug(Accnts);
}

It returns results. 

Cheers,
Aparna Hegde