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
Richa KRicha K 

SOQL Subquery

Hi All,

 

I need to get the new version of sObjects(Contacts) related to a Parent SObject(Account) in a map. I am doing like this :

 

trigger on Contact (before insert, before update) 
{
            set<Id> newConIds = new set<Id>();
            set<Id> newAccIds = new set<Id>();
            Map<Id,List<Contact>> MapAcc = new Map<Id,List<Contact>>();
            for(Contact c : Trigger.new)
            {
               newConIds.add(c.Id); 
               newAccIds.add(c.AccountId);
            }
            
            for(Account aa : 
[select Id,
(select Id,Title,LT_Last_Login_Date__c,Contact_Is_Primary__c,No_Of_Completed_Tasks__c from Contacts)
from Account where Id in : newAccIds]) { MapAcc.put(aa.Id,aa.Contacts); // Here I am getting the Old version of Contacts... Meaning, I am not getting the new values }

 

I want the map should have the new values of Contact records. Any suggestions?

Navatar_DbSupNavatar_DbSup

Hi,


Since your trigger on before insert and before update. So when you are making the SOQL with account and contact associated with that Account will always give you the old value because your trigger is on before. So you have to simply change it to after update then you will be able to get the new value.

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.