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
Learning Apex (OOP)Learning Apex (OOP) 

Variable does not exist - Why - Please explain the concept

Hello,
I am very new to programming and am learning to code with apex.
If you have experience in coding with apex, you will observe that the following code breaches many good practices' rules but, for now, I do not care (this is not about good practices, this question is about the error); I am just trying to understand WHY am I getting this error (Variable does not exit: Id)
I think it might have something to do with the "scope" of the code where this variable is being referenced but, I still need understand and to learn how to avoid these kind of issue in the future.

Could you possibly explain in plain English (for a beginner), why is this error being produced and how to avoid it?

Thank you very much. Here is the code:
trigger OwnerManager on Opportunity (after insert) {
    for(Opportunity opp : Trigger.new){
        //Get opp owner manager info
        Opportunity oppWithManagerInfo = [SELECT Id,
                                          Owner.ManagerId
                                          FROM Opportunity
                                          WHERE Id = :opp.Id];
        //Get opp owner direct dependents
        List<user> directDependents = [SELECT Id
                                       FROM user
                                       WHERE ManagerId = :opp.OwnerId
                                       LIMIT 1];
        //Create Opportunity Team Role
        if (oppWithManagerInfo.Owner.ManagerId != null) {
            OpportunityTeamMember otm = new OpportunityTeamMember();
            otm.UserId                = oppWithManagerInfo.Owner.ManagerId;
            otm.OpportunityId         = opp.Id;
            otm.TeamMemberRole        = 'Sales Manager';
            insert otm;
            //Create another Opportunity Team Role with directDependent
            if (directDependents.size()>0){
                OpportunityTeamMember otm2 = new OpportunityTeamMember();
                otm2.UserId                = directDependents.Id; // HERE is the error
                otm.OpportunityId          = opp.Id;
                otm.TeamMemberRole         = 'Sales Rep.';
                insert otm2;
            }
        }
    }
}

 
Best Answer chosen by Learning Apex (OOP)
Amit Chaudhary 8Amit Chaudhary 8
Your code should like below
trigger OwnerManager on Opportunity (after insert) {
    for(Opportunity opp : Trigger.new){
        //Get opp owner manager info
        Opportunity oppWithManagerInfo = [SELECT Id,
                                          Owner.ManagerId
                                          FROM Opportunity
                                          WHERE Id = :opp.Id];
        //Get opp owner direct dependents
        List<user> directDependents = [SELECT Id
                                       FROM user
                                       WHERE ManagerId = :opp.OwnerId
                                       LIMIT 1];
        //Create Opportunity Team Role
        if (oppWithManagerInfo.Owner.ManagerId != null) {
            OpportunityTeamMember otm = new OpportunityTeamMember();
            otm.UserId                = oppWithManagerInfo.Owner.ManagerId;
            otm.OpportunityId         = opp.Id;
            otm.TeamMemberRole        = 'Sales Manager';
            insert otm;
            //Create another Opportunity Team Role with directDependent
            if (directDependents.size()>0){
                OpportunityTeamMember otm2 = new OpportunityTeamMember();
                //otm2.UserId                = directDependents.Id; 
                otm2.UserId                = directDependents[0].Id; 
                otm.OpportunityId          = opp.Id;
                otm.TeamMemberRole         = 'Sales Rep.';
                insert otm2;
            }
        }
    }
}

NOTE:- directDependents was list so you need to get User record from directDependents then you can get ID.

Try to remove SOQL from for loop as well

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Your code should like below
trigger OwnerManager on Opportunity (after insert) {
    for(Opportunity opp : Trigger.new){
        //Get opp owner manager info
        Opportunity oppWithManagerInfo = [SELECT Id,
                                          Owner.ManagerId
                                          FROM Opportunity
                                          WHERE Id = :opp.Id];
        //Get opp owner direct dependents
        List<user> directDependents = [SELECT Id
                                       FROM user
                                       WHERE ManagerId = :opp.OwnerId
                                       LIMIT 1];
        //Create Opportunity Team Role
        if (oppWithManagerInfo.Owner.ManagerId != null) {
            OpportunityTeamMember otm = new OpportunityTeamMember();
            otm.UserId                = oppWithManagerInfo.Owner.ManagerId;
            otm.OpportunityId         = opp.Id;
            otm.TeamMemberRole        = 'Sales Manager';
            insert otm;
            //Create another Opportunity Team Role with directDependent
            if (directDependents.size()>0){
                OpportunityTeamMember otm2 = new OpportunityTeamMember();
                //otm2.UserId                = directDependents.Id; 
                otm2.UserId                = directDependents[0].Id; 
                otm.OpportunityId          = opp.Id;
                otm.TeamMemberRole         = 'Sales Rep.';
                insert otm2;
            }
        }
    }
}

NOTE:- directDependents was list so you need to get User record from directDependents then you can get ID.

Try to remove SOQL from for loop as well
This was selected as the best answer
Learning Apex (OOP)Learning Apex (OOP)
Hi Amit,

So, looking at your amendment on line 23 ...
otm2.UserId               = directDependents[0].Id;
... it looks like, due to the fact that I am referencing a List (directDependents is a List) it is NECESSARY to specify the postion of the item in the List, otherwise the system does not know what Id to assign to the variable otm2.userId.

This makes sense.

Thank you very much Amit.