0

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

Code

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;
        sObjectList.add(ld);

    }
    try{
        database.Insert(sObjectList);                 
        sObjectList.clear();
        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;
                sObjectList.add(ObjA);
            }   
            for(ObjectB__c ObjB  : doc.ObjBTypes){
                ObjB.Documentation_Detail__c = ld.Id;
                ObjB.Form_Id__c = formId;
                sObjectList.add(ObjB);
            }  

        }      

    }
    catch(exception ex){

        system.debug(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 '15 at 19:12
  • I have updated my post with code. – vraavi Dec 9 '15 at 19:45
2

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; }
}

UPDATE

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>();
recordsToInsert.addAll(aRecords);
recordsToInsert.addAll(bRecords);
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 '15 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 '15 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 '15 at 20:36
1

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 '15 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 '15 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 '15 at 20:05

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.