0

I am new to Apex trying to do some practice assignments and have gotten stuck on this junction object query.

Task: Edit the current getUser() method to do the following:

Return a custom object (that you define in the class) with the current getUser method's query data, and the list of teams the person belongs to (by querying the team assignments).

  • Parent Object 1: User (Standard Object)
  • Parent Object 2: Team (Custom Object)
  • Junction Object: Team Assignments (Custom Object) enter image description here
**Original Class & getUser Method**
global class CTI {
    global class CTIException extends Exception {}

    global static User getUser() { 
        return [SELECT Id, Name, ProfileId, Profile.Name, UserRoleId, UserRole.Name, Username 
                FROM User WHERE Id = :UserInfo.getUserId() ];
    }
}
1

Approach 1, return a User SObject with children that identify Teams

public static User getUdserData() {
   return [SELECT Id, UserName, ...,
            (SELECT Team__r.Name 
               FROM Team_Relationships__r)
            FROM User
            WHERE Id = UserInfo.getUserId()];
}

Approach 2 - return a custom type

public class UserData {
   public User u;
   public Team__c[] userTeams = new List<Team__c>();
}

public static UserData getUser() { 
    UserData ud = new UserData();
    ud.u = [SELECT Id, Name, ProfileId, Profile.Name, UserRoleId, UserRole.Name, Username 
            FROM User WHERE Id = :UserInfo.getUserId() ];
    for (Team_Assignment__c ta: 
      [SELECT Team__r.Name FROM Team_Assignment__c
         WHERE User.Id = :UserInfo.getUserId()]) {
      ud.userTeams.add(ta.Team__r.Name);
    }
    return ud;
}

So, a custom type is returned with the User and a list of Team__c

  • This did not work for me, I did edit the getUser() method to global static List<Team_Assignment__c> getUser() since the assignment said to query Team_Assigment__c and to return a custom object which Team Assignment is. But it was not defined in the class. – Patrick Shea Aug 2 at 1:41
  • I have gotten my base query but now just trying to add the rest List<Team_Assignment__c> userTeams = [SELECT Team__r.Name FROM Team_Assignment__c WHERE User__r.Id = :UserInfo.getUserId() ]; return userTeams; – Patrick Shea Aug 2 at 1:44
  • I'm sorry I misunderstood the question/your assignment. So, are you still having an issue now and if so, use the edit link to amend your question – cropredy Aug 2 at 2:11
  • Edited! Hopefully that helps make it more concise. I still need help. What I currently have is, ``` global static List<Team_Assignment__c> getUser() { User currentUser = [SELECT Id, Name, ProfileId, Profile.Name, UserRoleId, UserRole.Name, Username FROM User WHERE Id = :UserInfo.getUserId() ]; List<Team_Assignment__c> userTeams = [SELECT Team__r.Name FROM Team_Assignment__c WHERE User__r.Id = :currentUser.Id ]; return userTeams; } ``` – Patrick Shea Aug 2 at 2:34
  • ok, round 2 - not super clear on the assignment but between this and the previous answer you should have enough – cropredy Aug 2 at 2:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.