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
Evgenii GillEvgenii Gill 

how to write trigger that should check the length of the Description field; if the field is longer than 200 characters, you need to trim it to 197 characters and add a three-dot at the end of the line.

I am new in Apex, please help me to write trigger that should check the length of the Description field; if the field is longer than 200 characters, you need to trim it to 197 characters and add a three-dot at the end of the line.
Best Answer chosen by Evgenii Gill
Ajay K DubediAjay K Dubedi
Hi Evgenii,

Try the below code it will help you:

//Trigger code
 
trigger RestrictDescription on Task (before insert, before update) {
    RestrictDescriptionHelper.restrictDescriptionMethod(trigger.New);
}

//Helper class code

public class RestrictDescriptionHelper{
    public static void restrictDescriptionMethod(List<Task> taskList){
        for(Task taskObj : taskList){
            if(taskObj.Description != null && taskObj.Description.length() >= 200){
                String actualString = taskObj.Description;
                String trimString = actualString.substring(0, 197);
                trimString = trimString + '...';
                taskObj.Description = trimString;
            }
        }
    }
}




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

Thanks,
Ajay Dubedi
 

All Answers

Abdul KhatriAbdul Khatri
I believe you need to do that against Task Object. Do you have a different field where you don't need to show 200 character. Here is the sample trigger for your reference. If your Object and field are different you can change that.
 
trigger RestrictDescription on Task (before insert, before update) {

    for(Task task : trigger.new) {
    
        if(task.Description.length() >= 200)
        {
            task.Description = task.Description.left(197) + '...';
        }
    
    }

}

 
Ajay K DubediAjay K Dubedi
Hi Evgenii,

Try the below code it will help you:

//Trigger code
 
trigger RestrictDescription on Task (before insert, before update) {
    RestrictDescriptionHelper.restrictDescriptionMethod(trigger.New);
}

//Helper class code

public class RestrictDescriptionHelper{
    public static void restrictDescriptionMethod(List<Task> taskList){
        for(Task taskObj : taskList){
            if(taskObj.Description != null && taskObj.Description.length() >= 200){
                String actualString = taskObj.Description;
                String trimString = actualString.substring(0, 197);
                trimString = trimString + '...';
                taskObj.Description = trimString;
            }
        }
    }
}




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

Thanks,
Ajay Dubedi
 
This was selected as the best answer