+ Start a Discussion
mxalix258mxalix258 

Custom controller to display related records

So, what I'd like to do in my controller is query the current user id, pull the related contactid from that user, and display all Applications related to that Contact ID. (is there an easier way to do this?) Here is my code, but it doesn't seem to be working:

 

public class CurrentApps {
String userid = UserInfo.getUserId();
User firstuser;
    public List<User> getCurrentUser(){
        //Declare a new list
        List<User> CurrentUser = new List<User>();
        //Set the list equal to the results of the SOQL query
        CurrentUser = 
            [SELECT 
                FirstName, LastName, ContactId
            FROM User 
            WHERE Id =:userid ];
firstuser = CurrentUser.get(0);
        //Return the List
        return CurrentUser;
    }
    public List<Application__c> getUserApps(){
        //Declare a new list
        List<Application__c> UserApps = new List<Application__c>();
        //Set the list equal to the results of the SOQL query
        UserApps = 
            [SELECT 
                Name
            FROM Application__c 
            WHERE Contact__c =:firstuser.ContactId ];
        //Return the List
        return UserApps;
}
}

 

Best Answer chosen by Admin (Salesforce Developers) 
HusseyHussey

Hi,

 

You can update your code as follows.

 

public class CurrentApps {
  String userid = UserInfo.getUserId();
  public List<Application__c> getUserApps(){
     User CurrentUser = new User();
     CurrentUser = [SELECT FirstName, LastName, ContactId FROM User WHERE Id =:userid];
      //Declare a new list
      List<Application__c> UserApps = new List<Application__c>();
      //Set the list equal to the results of the SOQL query
      UserApps = [SELECT Name FROM Application__c WHERE Contact__c =:CurrentUser.ContactId ];
     //Return the List
   return UserApps;
  }
}

All Answers

HusseyHussey

Hi,

 

You can update your code as follows.

 

public class CurrentApps {
  String userid = UserInfo.getUserId();
  public List<Application__c> getUserApps(){
     User CurrentUser = new User();
     CurrentUser = [SELECT FirstName, LastName, ContactId FROM User WHERE Id =:userid];
      //Declare a new list
      List<Application__c> UserApps = new List<Application__c>();
      //Set the list equal to the results of the SOQL query
      UserApps = [SELECT Name FROM Application__c WHERE Contact__c =:CurrentUser.ContactId ];
     //Return the List
   return UserApps;
  }
}

This was selected as the best answer
mxalix258mxalix258

Works great...thank you!