Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I have a select list which lest the user select a sales rep Id. Based on that I want to display the Rep name in an outputtext. following is my controller code:

public  String repname {
        get {
           Integer regnInt = (regn!=null) ? Integer.valueOf(regn) : -1;               
           String Results = [Select Name
                             From User 
                             where Sales_Rep__c =: RepId limit 1];
           return Results ;
        }
        set;
}

RepId here is the output of selectlist. This code gives error: Illegal assignment from LIST to String.. How can I possibly select the repname here?

share|improve this question

3 Answers 3

up vote 1 down vote accepted

The APEX Select always return a List, so here's what you need to do:

List<User> Results = [Select Name
                      From User 
                      Where Sales_Rep__c =: RepId limit 1];
// Do some sanity check if necessary to make sure Results.size() is 1
return Results.get(0).Name;
share|improve this answer
    
@ Mingyu. thanks for the response but with this code, it gives an error that Illegal assignment from LIST<User> to LIST<String> I also tried changing it to LIST<User> Results = but then the return statement gives an error –  Robin Feb 17 at 17:08
1  
Select statements don't always return a list, they can return a singleton object too, and with dot notation you can extract a field from a SOQL in a single line. –  Simon Lawrence Feb 17 at 17:10
    
Using List<User> would work in this scenario, but then instead of "returning" the variable results, you would have to do something like return Results.get(0).name; –  Simon Lawrence Feb 17 at 17:13
    
@Robin, I updated my answer. –  Mingyu Feb 17 at 17:14
    
thanks..this worked perfectly fine.. :) –  Robin Feb 17 at 18:00

In this scenario, you can just add .name to the end of your query like so:

public  String repname {
   get {
   Integer regnInt = (regn!=null) ? Integer.valueOf(regn) : -1;               
   String Results = [Select Name From User where Sales_Rep__c =: RepId limit 1].name;
   return Results ;
  }
  set;
}

And the query will return a string for you into Results.

share|improve this answer
    
thanks Simon. This worked too. I guess both answers are slightly different ways of doing the same thing. Thanks again for the help.. –  Robin Feb 17 at 18:02
    
by the way a small help further. the rep name that gets returned is not formatted. I want First letter of both first name and last name to be capital and rest all letters to be small..is there a way to achieve this? –  Robin Feb 17 at 18:06
    
there is a code snippet in the middle of this discussion that might be of use to you: developer.salesforce.com/forums?id=906F00000008zVUIAY it takes a string, breaks it up over spaces, and then capitalises the first letter of each block (loosely). You should be able to implement this and then call it on your returned value in the controller. Looks like it might need a tweak or two though. –  Simon Lawrence Feb 18 at 9:26

All you need:

public  String repName {
  get {
    return [Select Name From User where Sales_Rep__c = :repId limit 1].Name;
  }
  set;
}

Formatting name (This assumes that there are at least 2 characters in both FirstName and LastName):

public String repName
{
  get
  {
    User rep = [SELECT FirstName, Lastname FROM User WHERE Sales_Rep__c = :repId LIMIT 1];
    return rep.FirstName.substring(0, 1).toUpperCase() + rep.FirstName.subString(1).toLowerCase() + ' ' + rep.LastName.substring(0, 1).toUpperCase() + rep.LastName.substring(1).toLowerCase();
  } set;
}
share|improve this answer
    
If you tell your VF page to only display this field if repId is populated, you won't need any checks in your apex. –  dphil Feb 17 at 18:39
    
thanks dphil...this works perfectly. However, the rep name is not formatted. Is there a way to convert the 1st letter of first name and last name to capital and rest all in lower case? –  Robin Feb 17 at 19:01
    
Well, this brings back the name as entered. If you are trying to format it that way, what happens when a last name is like McDonald? This can be a valid capitalization for a last name, but will make the last name incorrect if you do your formatting. The best bet is to make sure you enter the Users with the correct capitalization in names to begin with. If you still want to format it though, I can edit my answer. –  dphil Feb 17 at 19:25
    
yes please..the data is already residing in the system and can not be changed. Also the data gets updated through multiple sources so very difficult to monitor all. I agree with the example of McDonald but that would be one of very few. It will help a great lot if you can help convert the 1st letter to capital for both first and last name.. –  Robin Feb 17 at 19:32
    
Updated to add the second request. –  dphil Feb 17 at 19:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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