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.

In sObject, I have a field called Rep_Name which contains First name and Last name of the rep, separated by a space. However, the name is not formatted.. I want to convert 1st letter of each word to capital and rest to small. Is there a way to achieve this?

controller:

private string nameFormat(String repname1) {
List<String> elems = repname1.split(' ');
repname1= '';
for (String x : elems)
{
    repname1 += x.substring(0,1).toUpperCase()+x.substring(1,x.length()) + ' ';
}
return repname1;
}

public  String repname {
        get {
       Integer regnInt = (regn!=null) ? Integer.valueOf(regn) : -1;               

if( OSR != null){
    List<User> Results = [Select Name
                          From User 
                          Where Sales_Rep__c =: OSR limit 1];

    return nameFormat(Results.get(0).Name);
    }
    else {
    return null;
    }
  }
 set;
    } 
share|improve this question
add comment

1 Answer 1

I don't think Salesforce provides a method for that. You probably need a trigger before insert/update, the following code works for me:

String rep_name = 'david john mitch doe';
List<String> elems = rep_name.split(' ');
rep_name = '';
for (String x : elems)
{
    rep_name += x.substring(0,1).toUpperCase()+x.substring(1,x.length()) + ' ';
}
System.debug('>>>'+rep_name);
share|improve this answer
    
Hi brovasi. Thanks for the reply. I tried adding your snippet of the code. There was no compilation error but the fetching data gave an null pointer error. I have updated my question with the controller code. Can you help me with that? Thanks.. –  Robin Feb 18 at 8:35
    
you need to put lines 2 - 7 in their own method in your controller defined as private string nameFormat(String rep_name) {} then in your controller you need to return: return nameFormat(Results.get(0).Name); –  Simon Lawrence Feb 18 at 9:38
    
@Simon I have edited the controller code (see above). It doesnt throw an error but returns still returns the unformatted name. I am sure there is something very small that I am missing. Please help. Thanks –  Robin Feb 18 at 10:20
    
It works for me in a demo page and controller? What is OSR though, and regnInt, I assume these are handled else where in your controller? The capitalisation function definitely works, I am using it to load a User's name and capitalise the first two characters (by tweeking it) and it works great. –  Simon Lawrence Feb 18 at 10:27
add comment

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.