+ Start a Discussion
Anoop Patel 24Anoop Patel 24 

Invalid integer: common.apex.runtime.impl.SObjectList@c21a4303

I'm trying to count the number of accounts a user owns as well as stamping the last login date in a custom field within the same class but I'm receiving the error below on my scheduled class.

Invalid integer: common.apex.runtime.impl.SObjectList@c21a4303

My class...

global class LoginDateClass implements Schedulable{

    global void execute(SchedulableContext SC){
    
    list<user> userList = [SELECT Id,Total_Number_of_Accounts__c,LastLoginDate FROM User WHERE IsActive = TRUE LIMIT 9999];
    LIST<AggregateResult> NoOfAccounts = [SELECT OwnerId u, count(Id) a FROM Account WHERE CoreAccounts__Inactive__c = FALSE  AND OwnerId in :userList GROUP BY OwnerId];
    
    
    for(User user : userList){
        user.Last_Login__c = user.LastLoginDate;
        user.Total_Number_of_Accounts__c  = integer.valueOf(NoOfAccounts);
        }
        update userList;   
      }
}
Best Answer chosen by Anoop Patel 24
SijuSiju
Please try the below code
global class LoginDateClass implements Schedulable{

    global void execute(SchedulableContext SC){
    
    list<user> userList = [SELECT Id,Total_Number_of_Accounts__c,LastLoginDate FROM User WHERE IsActive = TRUE LIMIT 9999];
    LIST<AggregateResult> NoOfAccounts = [SELECT OwnerId, count(Id) FROM Account WHERE CoreAccounts__Inactive__c = FALSE  AND OwnerId in :userList GROUP BY OwnerId];
     Map<id,Integer>  NoOfAccountsMap = new Map<id,Integer>();
	 Integer Total;
     String UserID;
	 
	for(AggregateResult AR1: NoOfAccounts ){
		 Total = Integer.valueOf(AR1.get('expr0')); 
		 UserID = String.valueOf(AR1.get('OwnerId'));
		 NoOfAccountsMap .put(UserID ,Total );
    } 
	
    list<user> userListToUpdate = new List<User>()
    for(User user : userList){
		if(NoOfAccountsMap.get(user.ID)>=1){// Check the user has any accounts owned 
			user.Total_Number_of_Accounts__c =NoOfAccountsMap.get(user.ID);
			user.Last_Login__c  = user.LastLoginDate;
			userListToUpdate.add(user);
		}
	}
       update userListToUpdate;   
    }
}

All Answers

SijuSiju
Please try the below code
global class LoginDateClass implements Schedulable{

    global void execute(SchedulableContext SC){
    
    list<user> userList = [SELECT Id,Total_Number_of_Accounts__c,LastLoginDate FROM User WHERE IsActive = TRUE LIMIT 9999];
    LIST<AggregateResult> NoOfAccounts = [SELECT OwnerId, count(Id) FROM Account WHERE CoreAccounts__Inactive__c = FALSE  AND OwnerId in :userList GROUP BY OwnerId];
     Map<id,Integer>  NoOfAccountsMap = new Map<id,Integer>();
	 Integer Total;
     String UserID;
	 
	for(AggregateResult AR1: NoOfAccounts ){
		 Total = Integer.valueOf(AR1.get('expr0')); 
		 UserID = String.valueOf(AR1.get('OwnerId'));
		 NoOfAccountsMap .put(UserID ,Total );
    } 
	
    list<user> userListToUpdate = new List<User>()
    for(User user : userList){
		if(NoOfAccountsMap.get(user.ID)>=1){// Check the user has any accounts owned 
			user.Total_Number_of_Accounts__c =NoOfAccountsMap.get(user.ID);
			user.Last_Login__c  = user.LastLoginDate;
			userListToUpdate.add(user);
		}
	}
       update userListToUpdate;   
    }
}
This was selected as the best answer
Anoop Patel 24Anoop Patel 24
Hi SIJu this worked thank you.
Jason Quevauvilliers 11Jason Quevauvilliers 11
I am also getting the same error when trying to print a Map to a visualforce page. in the controller the map prints out fine:

{
    null = (),
    0018E00000e7d0FQAQ = (Required_Document__c: {
        Name = Secondary Guardian Identity Documents,
        Status__c = Verified,
        Related_Party__c = 0018E00000e7d0FQAQ,
        Document_Type__c = a2b8E000000Xn8oQAC,
        Id = a2g8E000000Tbs9QAC
    }, Required_Document__c: {
        Name = Primary Guardian Identity Documents,
        Status__c = Required,
        Related_Party__c = 0018E00000e7d0FQAQ,
        Document_Type__c = a2b8E000000Xn8pQAC,
        Id = a2g8E000000TdlqQAC
    }, Required_Document__c: {
        Name = Proof of residence for the primary caregiver,
        Status__c = Required,
        Related_Party__c = 0018E00000e7d0FQAQ,
        Document_Type__c = a2b8E000000Xn8uQAC,
        Id = a2g8E000000TdlrQAC
    }),
    0018E00000f8JvIQAU = (),
    0018E00000f8JvgQAE = (Required_Document__c: {
        Name = Primary Guardian Identity Documents,
        Status__c = Required,
        Related_Party__c = 0018E00000f8JvgQAE,
        Document_Type__c = a2b8E000000Xn8pQAC,
        Id = a2g8E000000TdlvQAC
    }, Required_Document__c: {
        Name = Proof of residence for the primary caregiver,
        Status__c = Required,
        Related_Party__c = 0018E00000f8JvgQAE,
        Document_Type__c = a2b8E000000Xn8uQAC,
        Id = a2g8E000000TdlwQAC
    }),
    0018E00000f8K3KQAU = ()
}

But on the page if i print the map i get:

{
    0018E00000f8JvIQAU = common.apex.runtime.impl.SObjectList @ 1,
    null = common.apex.runtime.impl.SObjectList @ 1,
    0018E00000e7d0FQAQ = common.apex.runtime.impl.SObjectList @ cf1961b0,
    0018E00000f8JvgQAE = common.apex.runtime.impl.SObjectList @ 722da41e,
    0018E00000f8K3KQAU = common.apex.runtime.impl.SObjectList @ 1
}

What was the solution that you used? and would it work for my case?