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
Pranil SarodePranil Sarode 

How to remove last 3 digits in account name

I want to remove last 3 digits , characters in account name.
Khan AnasKhan Anas (Salesforce Developers) 
Hi Pranil,

Greetings to you!

You can use the below trigger:
trigger TrimAccName on Account (before insert, before update) {

    for( Account acc : Trigger.new){
        String accName = acc.Name;
        acc.Name = accName.substring(0,accName.length()-3);
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Kumar, GajendraKumar, Gajendra
Hi,

Try below:
 
String s = 'MyAccount';
system.debug(s.substring(s.length()-3,s.length()));

Regards,
Gajendra

If this answers your issue then please mark it as best answer.
Ajay K DubediAjay K Dubedi
Hi Pranil,

You can use the below code.

I have written a trigger as per your requirement.  

---Trigger ---
trigger AccountDeletion on Account (before insert, before update) {
    if(  trigger.isBefore && (trigger.isInsert || trigger.isUpdate )){ 
        DeleteLast_ThreeDigits.deleteAccountLastThreeDigits(trigger.new);
    }
}
---Apex Class---
public class DeleteLast_ThreeDigits {
    
    public static void deleteAccountLastThreeDigits(List<Account> accountList){
        try{
            for( Account acc : accountList){
                if(acc.Name.length() >3){
                String accName = acc.Name;
                acc.Name = accName.substring(0,accName.length()-3);
                }
            }
        }catch(Exception exp){
            System.debug('Exception Cause-->>'+exp.getMessage()+'Line Number-->>'+exp.getLineNumber());
        }
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com