+ Start a Discussion
babloo123babloo123 

Apex code not working

Hi All I am looking to get the contact that is associated with the user who logged in and redirect to the detail page. But it is not throwing any error but I am not able to redirect properly can some one help where the code is wrong?

public class MyContact {

    public PageReference redirect() {
    
   User u=[Select contactid from User where id=:UserInfo.getUserId()];
        system.debug(u);
    
system.debug(u.contactid);
PageReference pageRef = new PageReference('/' + 'u.contactid');
pageref.setredirect(True);
        return pageref;
    }


public MyContact(){}




}
James LoghryJames Loghry
How are you calling your redirect method?  Are you redirecting on page load?  Or are you redirecting after a button is clicked?  Does your redirect method even get called?  What are the outputs of your two System.debug calls?  All of the above could help in figuring out what the exact issue is.
Roy LuoRoy Luo
The problem is in the rediect url 
PageReference pageRef = new PageReference('/' + 'u.contactid');

Should not put u.contactid inside single quote, that would just give you the string iteself not the id value.
public class MyContact {

    public PageReference redirect() {
    
   User u=[Select contactid from User where id=:UserInfo.getUserId()];
        system.debug(u);
    
system.debug(u.contactid);
PageReference pageRef = new PageReference('/' + u.contactid);
pageref.setredirect(True);
        return pageref;
    }


public MyContact(){}




}