Debug Log Screenshot

When sorting above records of Object A and Object B in sobject list, i was getting below error. Initially I thought this is some thing related to values in Row_Index__c number type field but now i realized it could be some thing else causing this issue.

System.UnexpectedException: java.math.BigDecimal cannot be cast to java.lang.Integer


public static void getObjAAndLienJudgement(list<documentationClass> lDocs, string formId){

    list<SObject> sObjectList = new list<SObject>();

    for(DocumentationClass doc :lDocs){

        Documentation_Detail__c ld = doc.DocumentationDetail;
        ld.Form_Id__c = formId;

        for(DocumentationClass doc :lDocs){

            Documentation_Detail__c ld = doc.DocumentationDetail;

            for(ObjectA__c ObjA  : doc.objATypes){
                ObjA.Documentation_Detail__c = ld.Id;
                ObjA.Form_Id__c = formId;
            for(ObjectB__c ObjB  : doc.ObjBTypes){
                ObjB.Documentation_Detail__c = ld.Id;
                ObjB.Form_Id__c = formId;


    catch(exception ex){


    sObjectList.sort(); // right here system is throwing the exception.

    insert sObjectList;

  • 1
    Do you have any code you could share?
    – Adrian Larson
    Dec 9, 2015 at 19:12
  • I have updated my post with code.
    – vraavi
    Dec 9, 2015 at 19:45

3 Answers 3


If you are getting an error when sorting the List<SObject>, you may want to implement the Comparable interface on your wrapper class and sort your wrappers instead. It is not immediately clear based on your OP why you need to sort these things at all, so removing the sort call may be a more appropriate (and certainly simpler) course of action.

If you must sort, you can tweak the implementation logic below as needed to get the desired sort order, but the basic idea would look something like the following.

public class DocumentationClass implements Comparable
    public Documentation_Detail__c documentationDetail { get; private set; }
    public Integer compareTo(Object instance)
        DocumentationClass that = (DocumentationClass)instance;
        if (this.getComparisonValue() > that.getComparisonValue())
            return 1;
        else if (this.getComparisonValue() < that.getComparisonValue())
            return -1;
        return 0;
    String getComparisonValue() { return documentationDetail.Form_Id__c; }


After looking at your code more closely, you should just do the following:

List<ObjectA__c> aRecords = new List<ObjectA__c>();
List<ObjectB__c> bRecords = new List<ObjectB__c>();
for (DocumentationClass doc : allDocs)
    // add aRecords
    // add bRecords
List<SObject> recordsToInsert = new List<SObject>();
insert recordsToInsert;

In this way, you can keep your records grouped by sObjectType without ever needing to worry about sort order.

  • it is not required to sort that sobject list, the only reason i was doing is to avoid following error "Cannot have more than 10 types in a single save operation. Please reduce number of types in one save operation."
    – vraavi
    Dec 9, 2015 at 20:04
  • They're keeping the "chunks" together, so I'd also recommend not sorting, but if they were using some crazy scheme where ObjectA__c and ObjectB__c were interspersed, sorting may be desirable to avoid a "too many chunks" error.
    – sfdcfox
    Dec 9, 2015 at 20:05
  • Thanks Adrian. Initially i was inserting those records in similar fashion, later in the process of code refactoring i thought of adding records directly in to a sobject list an to avoid "too many chunks" error i sorted sobject list.
    – vraavi
    Dec 9, 2015 at 20:36

It's been many years since this question was first posted, but I ran into this very same issue last year, and was able to create a trivial reproduction that always throws the BigDecimal error:

 * Throws the following error:
 * System.UnexpectedException: class java.math.BigDecimal cannot be cast to class java.lang.Integer
 * (java.math.BigDecimal and java.lang.Integer are in module java.base of loader 'bootstrap')
 * never ever fill out the Name field here; there's a reason it's blank
 * and it would cause this test to pass as a false positive

Account one = new Account();
Account two = new Account();

// behavior is specific to .put - initializing the fields
// directly doesn't lead to this issue
one.put(Account.AnnualRevenue, 0);
one.put(Account.NumberOfEmployees, 0);
two.put(Account.AnnualRevenue, 0);
two.put(Account.NumberOfEmployees, 0);

// division coerces the decimal to an integer, but also leads to
// sort blowing up!
one.AnnualRevenue = (4.00 / 2);

new List<Account>{ one, two }.sort();

System.assert(true, 'Should make it here'); // if .sort() throws, it's an uncatchable error

The solution can be a few different things; one possible option would be to coerce values for any given field for records in a list to their proper type prior to sorting. The most performant one, however, is Adrian's original answer - by creating a Comparable implementation, we bypass the default .sort() behavior altogether and avoid this issue entirely.


You can't cast decimals to integers. While I'm not able to replicate that exact error, I managed to get its close cousin:

System.TypeException: Invalid conversion from runtime type Decimal to Integer

Here's the code I wrote to generate the error.

Decimal a = 123123234.12321321321;
Object b = a;
Integer c = (Integer)b;

You'll have to convert the Decimal to an Integer first via intValue():

Decimal a = 123123234.12321321321;
Object b = a;
Integer c = ((Decimal)b).intValue();

Like other functions that return part of a value (e.g. DateTime's date() function), you'll lose some of the data-- the fractional part of the decimal value.

  • I wouldn't say it's obvious you lose what's after the decimal point. A different rounding scheme could be used instead of truncation to get the nearest value.
    – Adrian Larson
    Dec 9, 2015 at 19:49
  • @AdrianLarson True enough, so I edited this. The platform is, at least in most respects, consistent. However, I'm probably going to have to delete this answer anyways, because it's only tangential to whatever's actually going on.
    – sfdcfox
    Dec 9, 2015 at 20:00
  • I think it's still helpful for context even if it doesn't address why sort would fail.
    – Adrian Larson
    Dec 9, 2015 at 20:05

You must log in to answer this question.

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