1

I am getting the titled error

System.StringException: Ending position out of bounds:99

for the below code:

CusObj2.ProdName__c=CusObj2.Name__c.substring(0,99);

All I want to do is truncate the name in this trigger to 99 characters or fewer. Please advise.

Pls note I am getting this error only when deploying a change set - it passes the same testClass in my sandbox.

1 Answer 1

6

The abbreviate method is a good choice:

CusObj2.ProdName__c = CusObj2.Name__c.abbrevate(99);

as it:

Returns an abbreviated version of the String, of the specified length and with ellipses appended if the current String is longer than the specified length; otherwise, returns the original String without ellipses.

where ellipses means three dots.

PS

Or if Name__c can be null:

CusObj2.ProdName__c = CusObj2.Name__c != null ? CusObj2.Name__c.abbrevate(99) : null;
3
  • 3
    string.left(99) if you don't care about the ellipses.
    – gNerb
    Jan 10, 2020 at 21:49
  • what if the string is null?
    – jaw999
    Jan 10, 2020 at 21:51
  • Or, more likely in most use cases - CusObj2__c.ProdName__c = CusObj2__c.Name__c.abbreviate(CusObj2__c.ProdName.getDescribe().getLength()); (ex the null check)
    – cropredy
    Jan 11, 2020 at 1:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .