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.

up vote 6 down vote accepted

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.

  • 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. – Cuban coffee Aug 17 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 Aug 17 at 9:57

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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