0

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.

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
  • 2
    string.left(99) if you don't care about the ellipses. – gNerb Jan 10 '20 at 21:49
  • what if the string is null? – jaw999 Jan 10 '20 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 '20 at 1:27

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.