[SalesForce] why am I getting a null value on return of this

UPDATE

Big thanks to everyone who chipped in to help me out!

pulling my hair out. Anyone spot the issue? Am I going nuts or is it just because its at the end of the day?

public id User {get;set;}
public User ActiveUser {get;set;}
// public string UserName {get;set;}
// public string useremail {get;set;}
// public string useremail {get;set;}
String userEmail;
String userWWID;


public void getUserName(){
    String userName = UserInfo.getUserName();

    User activeUsers  = [SELECT Email, WWID__c FROM User WHERE Id=:UserInfo.getUserId()]; //Id=:UserInfo.getUserId()
    List<User> Users = [SELECT Id, WWID__c FROM User WHERE Id=:userinfo.getUserId() LIMIT 1];
    String userWWID = Users[0].Name;
}

public PageReference PageRedirect() {
    string host = ApexPages.currentPage().getHeaders().get('host');
    string url = ApexPages.currentPage().getUrl();
    String ParentId = System.currentPageReference().getParameters().get('parameter');
    PageReference PageRedirect = new PageReference('https://webbsite/Home.do' + userWWID);
    PageRedirect.setRedirect(true);
    return PageRedirect;
}

Best Answer

Your code as written uses UserWWID in the redirect, which is set in GetUserId(), but that method is never called. If it were called, you'd get an error on the last line of GetUserId(), since you are trying to reference the Name field, which wasn't included in your query. Here's a simplified version of your code which I think will work, but I'm making the assumption that the field you need in your constructed URL is actually User.WWID__c.

// just store a reference to the user, then use
// activeUser.name, activeUser.id, activeUser.wwid__c, etc.
// as needed in code or VF
public User activeUser {get;set;}

// Constructor: name should match your class name
public ControllerName(){
    User activeUser  = [ SELECT id, Name, Email, WWID__c 
                           FROM User 
                          WHERE Id=:UserInfo.getUserId()];
}

public PageReference PageRedirect() {
    system.debug(activeUser);
    if (activeUser.wwid__c == null) {
        return null;
    }
    string targetUrl = 'https://webbsite/Home.do/' + activeUser.wwid__c;
    return new PageReference(targetUrl);
}

A few notes:

  • As @CasparHarmer noted in his answer, you should move your setup into a constructor method, so it always runs when your controller is created. Constructors have no return type, and the name must match your class name, so change ControllerName in the sample code above.
  • I'm assigning a query result directly to an sObject (activeUser). This is nearly always bad, since a query returning 0 or >1 records will throw an error, however, querying user where id = :currentUserId is always safe in a controller, since there's always a user. Just be sure the user has FLS on the WWID__c field, or it will come back null. Since activeUser is a public property, you can stick {!activeUser.wwid__c} in your VF page temporarily for debugging, to make sure your are getting a wwid__c value.
  • Check the targetUrl line. Your code didn't have / or a ?param= at the end of the URL; I'm guessing you need one or the other.
  • I omitted the setRedirect(); per the docs it isn't needed when the URL refers to an external website.

Update: I've updated the PageRedirect() method, so that if User.WWID__c is null, the method returns null. Since you are calling this as an action on your VF Page, if the method returns null, it doesn't redirect, and just outputs the VF Page. Along with this change, add something to your VF page to show what happened, since it will only be shown if the redirect fails. For example:

<apex:page controller=...>
    User WWID__c:  {!user.wwid__c}
</apex:page>

Also added a debug statement to output the user data.

Related Topic