function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
kalyan chandra 9kalyan chandra 9 

hi , i am writng a trigger to make the letter in every word in account name to be upper case. here is the trigger and apex class i have used. its working but the code is adding parenthesis every time i insert or update

trigger accountnameuppercase on account (before insert,before Update) {
    List<account> acclist = trigger.new;
    nameupper.makeuppercase(acclist);
}

apex class:::

public class accnameuppercase{
    public static void makeuppercase(List<account> acclist){
        for(account a : acclist){
       String rep_name =a.name;
List<String> elems = rep_name.split('');
rep_name = '';
for (String x : elems)
{
    rep_name += x.substring(0,1).toUpperCase()+x.substring(1,x.length() ) + ' ';
}  

   a.name=rep_name;
   
        }
        }
        }
kalyan chandra 9kalyan chandra 9
User-added image
Shingo YamazakiShingo Yamazaki
Hi, kalyan

I copied your code and tried it but caused an error,
so I'm not sure the exact reason why it adds parenthesis.
(You are missing a blank space in "rep_name.split('');" ?)

Anyway, this code works, does it make sense?
public static void makeuppercase(List<account> acclist){
  for(account a : acclist){
    String name  = a.name;
    List<String> words = name.split(' ');
    List<String> repWords = new List<String>();

    for (String word : words) {
      repWords.add(word.capitalize());
    }
  a.name = String.join(repWords, ' ');
}


 
SantoshChitalkarSantoshChitalkar
Hi Kalyan,

Use following code to solve your problem. For some reason your code is not working. I am not able to debug it .
 
trigger accountnameuppercase on account (before insert,before Update) {
    for(Account account : trigger.New){
        String aName = account.Name;
        account.Name = aName.toUpperCase();
    }
}

Regards,
Santosh
SantoshChitalkarSantoshChitalkar
mark this question as solved if your issue is resolved
kalyan chandra 9kalyan chandra 9
its '  ' not "".anyway its working for me
kalyan chandra 9kalyan chandra 9
both codes are not working.
Shingo YamazakiShingo Yamazaki
I missed one right-bracket }.
 
public class accnameuppercase {
    public static void makeuppercase(List<account> acclist) {
        for(account a : acclist) {
            String name  = a.name;
            List<String> words = name.split(' ');
            List<String> repWords = new List<String>();
    
            for (String word : words) {
                repWords.add(word.capitalize());
            }
            a.name = String.join(repWords, ' ');
        }
    }
}

 
doravmondoravmon

just add a if statement


apex class:::

public class accnameuppercase{
    public static void makeuppercase(List<account> acclist,Map<Id,Account> oldMap){
        for(account a : acclist){
          //if a. name is not capitalize, then dosomething, else do nothing
}
Devendra NataniDevendra Natani
Hello Kalyan,

You can use the following code.
trigger accountnameuppercase on account (before insert,before Update) {

for(Account account : trigger.New){
        account.Name = account.Name.toUpperCase();
    }

}


Thanks,

Devendra

RaggyRaoRaggyRao
Hi KalyanChandra,

What worked for you? I need to know if any of the above suggested answers worked for you... please do let me know!!

Shingo Yamazaki / Santosh Chitalkar : Do your codes work when you tested them in your Org?
Im trying to use one of them  :p

Thanks
#RaggyRao