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
KRISHNAMURTHY ANANTHARAMAKRISHNANKRISHNAMURTHY ANANTHARAMAKRISHNAN 

Map and dot notation

I am trying to learn Apex and a newbie at this. I am trying to do the following trailhead:

https://trailhead.salesforce.com/trails/force_com_dev_beginner/modules/apex_triggers/units/apex_triggers_intro

In the example, they provide the following code:
// Get the related opportunities for the accounts in this trigger
Map<Id,Account> acctsWithOpps = new Map<Id,Account>( [SELECT Id,(SELECT Id FROM Opportunities) FROM Account WHERE Id IN :Trigger.New]);

I don't understand why map is declared of type Account but then the subquery (SELECT Id FROM Opportunities) returns IDs of type opportunities?

Second question:
System.debug('acctsWithOpps.get(a.Id).Opportunities.size()=' + acctsWithOpps.get(a.Id).Opportunities.size());


Since opportunities is a child object to account, why do we use get(a.id).opportunities because I thought the dot notation was to traverse from child to parent?
sachinarorasfsachinarorasf
Hi KRISHNAMURTHY,

Map<Id,Account> acctsWithOpps = new Map<Id,Account>( [SELECT Id,(SELECT Id FROM Opportunities) FROM Account WHERE Id IN :Trigger.New]);

In the above code, This map is created to get all the account id and accounts that have related Opportunities to it, initially, the accounts that have been queried doesn't have any Opportunities related to them
because we are getting the accounts with no Opportunities 

See this code :

 for(Account a : Trigger.New) {
        System.debug('acctsWithOpps.get(a.Id).Opportunities.size()=' + acctsWithOpps.get(a.Id).Opportunities.size());
        // Check if the account already has a related opportunity.
        if (acctsWithOpps.get(a.Id).Opportunities.size() == 0) {
            // If it doesn't, add a default opportunity
            oppList.add(new Opportunity(Name=a.Name + ' Opportunity',
                                       StageName='Prospecting',
                                       CloseDate=System.today().addMonths(1),
                                       AccountId=a.Id));
        }           
    }
    if (oppList.size() > 0) {
        insert oppList;
    }
    In the above code, we get all the accounts that are newly created we are checking the opportunity size related to that account. if the opportunity size is zero then we are creating the new opportunity related to that account with all required fields and then adding to a new opportunity list and insert that opportunity list.

I hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too.

Thanks and Regards,
Sachin Arora
www.sachinsf.com
KRISHNAMURTHY ANANTHARAMAKRISHNANKRISHNAMURTHY ANANTHARAMAKRISHNAN
Hi Sachin, 

Can you explain the below syntax step by step?
acctsWithOpps.get(a.Id).Opportunities.size()

Also, how are we using the size method magivally in the above statement?