STRING_TOO_LONG “data value too large” Exception in Salesforce

STRING_TOO_LONG “data value too large” Exception in Salesforce

STRING_TOO_LONG “data value too large” Exception in Salesforce occurs due to populating a field with more than its maximum length.

For example, if a field length is set to 255 and if we try to populate more than 255 characters, this exception will be thrown.

As it is an exception from the Database, the records won’t be committed to the database.

Sample Code to reproduce the issue:

String strTemp = 'TestTestTest';
strTemp += 'TestTestTest';
strTemp += 'TestTestTest';
strTemp += 'TestTestTest';
strTemp += 'TestTestTest';
strTemp += 'TestTestTest';
strTemp += 'TestTestTest';
System.debug(
    'strTemp length is ' +
    strTemp.length()
);
Account objAccount = new Account();
objAccount.Name = 'Testing';
objAccount.Text__c = strTemp;
objAccount.Description = 'Testing Limit';
try {
    insert objAccount;
} catch( DMLException e ) {
    System.debug(
        'Exception is ' +
        e.getMessage()
    );
}

Leave a Reply