You need to sign in to do that
Don't have an account?
Sorting visualforce tables - reference fields
Hello!
I am referring to the code sample on Sorting Tables (apex tables) found here: http://wiki.developerforce.com/index.php/Sorting_Tables.
Now, even though this article says that code coverage with the included testmethods should be 100%, I am not getting this result. It comes to about 58% on the class superSort.
The lines not being tested are:
if(items[0].getSObjectType().getDescribe().fields.getMap().get(sortField).getDescribe().getType().Name() == 'REFERENCE'){ isSortFieldReference = true; referenceName = new Map<Id,String>(); /*Determine the type of this object and populate the Id to Name map*/ Set<Id> referenceIds = new Set<Id>(); for(sObject s : items){ referenceIds.add((Id)s.get(sortField)); } String objectID = (String)items[0].get(sortField); String prefix = objectID.substring(0,3); String objectType; Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe(); for(Schema.SObjectType s : gd.values()){ if(prefix == s.getDescribe().getKeyPrefix()){ objectType = s.getDescribe().Name; } } //Query the related objects for the name and populate the Id -> Name map String queryString = 'select Id, Name from ' + objectType + ' where ID IN :referenceIDs'; for(sObject s : Database.query(queryString )){ referenceName.put((Id)s.get('Id'),(String)s.get('Name')); } }
And the test method I am using is:
public static testMethod void relatedSortTest(){ List<Opportunity> opps = new List<Opportunity>(); Account a = new Account(); for(integer i = 0; i<1000; i++){ opps.add(new Opportunity(Name = 'test' + i, Amount = 1, Account = a)); } Test.startTest(); sortList(opps,'Accountid','desc'); Test.stopTest(); }
Now, I believe the purpose of this piece of code is to allow sort on even a reference field which is why I am testing with Account on Opportunities, but somehow this is always throwing the following error message:
NullPointerException: Attempt to de-reference a null object - at this line - String prefix = objectID.substring(0,3);
Has anyone encountered this situation, or have a solution to this?
Thanks,
Pranav