1

We deployed the below trigger from sandbox to production using outbound change set. No apex or test classes were created. Intent of trigger is to pass the first 255 characters from a Task Description field to a custom field that can be used in an Activity List View.

Trigger works as expected for system admin and highest role of role tree. All other users encounter the following error:

Validation Errors While Saving Record(s) There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger PassLongField caused an unexpected exception, contact your administrator: PassLongField: execution of BeforeUpdate caused by: System.StringException: Ending position out of bounds: 255: ()".

Click here to return to the previous page.

Any support would be really appreciated. Have no clue how to proceed. Thanks

trigger PassLongField on Task (before update) {

    for (Task Task:System.Trigger.new){ 
        //transfer long text field
        if(Task.Description != NULL){
            Task.List_View_Comments__c = String.valueOf(Task.Description).substring(0,255);
        }
    }
}
3

Substring should throw this error all the time if the length does not exceed 255. Instead, consider using the abbreviate method instead, which will truncate to (size-3), and add "..." to the end if the length is exceeded, and will not throw an error if the string is shorter:

Task.List_View_Comments__c = Task.Description.abbreviate(255);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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