+ Start a Discussion
JoeyDJoeyD 

Using custom field from User for SOQL query

Hi all,

 

I'm currently using the following class:

 

public with sharing class classTest{
    
    public classTest(ApexPages.StandardSetController controller) {
    }
    
    List<Dealer_Forecast__c> dfc;
    public String myUserID = UserInfo.getUserId();

    public List<Dealer_Forecast__c> getDfc(){
        if(dfc == null){
            dfc = [SELECT Account__c,Style__c, Region__c, Territory__c
                FROM Dealer_Forecast__c 
                WHERE Account__r.OwnerId =:myUserID];
        }    
        
        return dfc;
    }
}
    

 

 

 

But instead of creating the list where the owner of the account is the current user, I'd like to pull records where a custom field on the related Account = a custom field on the current user's User record. So I changed the code to the following:

 

public with sharing class classTest{
    
    public classTest(ApexPages.StandardSetController controller) {
    }
    
    public String userlist {get; set;}
    List<Dealer_Forecast__c> dfc;
    public String myUserID = UserInfo.getUserId();

    public String getUserlist(){  
        userlist = [SELECT Report_Server_Co_Id__c FROM User WHERE id = :myUserID LIMIT 1].Report_Server_Co_Id__c;
        return userlist;   
    }
    public Integer userterr = Integer.valueOf(userlist);
    
    public List<Dealer_Forecast__c> getDfc(){
        if(dfc == null){
            dfc = [SELECT Account__c,Style__c, Region__c, Territory__c
                FROM Dealer_Forecast__c 
                WHERE Account__r.Territory__c =:userterr];
        }    
        
        return dfc;
    }
}
    

 

 

 

I am able to save the class, but when the code is executed, I get an error stating "Argument 1 cannot be null". I imagine the way I'm trying to achieve what is I need is completely incorrect.

 

Any suggestions?

Thanks!

 

SuperfellSuperfell

Noithing calls getuserlist, so userlist is always null.

MandyKoolMandyKool

Hi there,

 

Can you please let us know, on which line you are getting this error?

From this code it looks like the problem is in following code

 

public String getUserlist(){  
        userlist = [SELECT Report_Server_Co_Id__c FROM User WHERE id = :myUserID LIMIT 1].Report_Server_Co_Id__c;
        return userlist;   
    }
JoeyDJoeyD

Unfortunately I don't know which line.  The error that displays when attempting to view the visualforce page simply says "Argument 1 cannot be null. An unexpected error has occurred. Your development organization has been notified."  I guess in the sandbox I'm not considered the development organization, because I haven't received any of the emails from the errors. :smileyindifferent:

 

Thank you for the input SimonF, but to be honest I don't know how to call something.  I've been playing with apex for only a few weeks, and I've never done any sort of programming before, so I'm incredibly inexperienced. I've tried reading the developers guides and tutorials, but everything just goes over my head, so I'm trying to learn from trial and error. Or rather "trial, error, google, trial, error, google, trial error, google, ask on the forums" :smileytongue:

 

I appreciate the help

fgwarbfgwarb

SimonF was spot on.  This is your line with the error:

 

public Integer userterr = Integer.valueOf(userlist);

 

 

userlist is null, so Integer.valueOf is failing, because Argument 1 == null.

 

This could be a solution for you:

 

public with sharing class classTest{
    
    public classTest(ApexPages.StandardSetController controller) {
        userterr = Integer.valueOf(getUserList());
    }
    
    public String userlist {get; set;}
    List<Dealer_Forecast__c> dfc;
    public String myUserID = UserInfo.getUserId();

    public String getUserlist(){  
        userlist = [SELECT Report_Server_Co_Id__c FROM User WHERE id = :myUserID LIMIT 1].Report_Server_Co_Id__c;
        return userlist;   
    }
    public Integer userterr;
    
    public List<Dealer_Forecast__c> getDfc(){
        if(dfc == null){
            dfc = [SELECT Account__c,Style__c, Region__c, Territory__c
                FROM Dealer_Forecast__c 
                WHERE Account__r.Territory__c =:userterr];
        }    
        
        return dfc;
    }
}