4

I received an exception while trying to insert an order item. It appears to be that the description field allows only 255 characters; sometimes my descriptions goes beyond 255.

I will shorten the length of the description before inserting the entry. See the code as follows:

Order ord = new Order(.....);

String description = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, ';

if(description.length() > 255){
    description = description.abbreviate(255);
}

ord.Description = description;

insert ord;

I am wondering whether there is an easier way to accomplish this by using the Salesforce built-in features. Meaning without writing code.

1 Answer 1

6

You can use the allowFieldTruncation header in the DMLOptions Class.

Order record = new Order(...);
Database.DMLOptions opt = new Database.DMLOptions();
opt.allowFieldTruncation = true;
Database.insert(record, opt);

If you're dong this from elsewhere (e.g. the API), there are similar headers you can use for allowFieldTruncation to prevent errors from appearing when field values are too long, without additional code.

2
  • I see. Is there a way to specify a concrete field? I am assuming that by using the DMLOption approach will truncate all the fields if required. Commented Aug 17, 2018 at 9:56
  • 1
    @Cubancoffee Yes, it will allow all fields to truncate. If you need more specific behavior, you would have to do it one-off.
    – sfdcfox
    Commented Aug 17, 2018 at 9:57

You must log in to answer this question.

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