+ Start a Discussion
Andrew TelfordAndrew Telford 

Add List Values to a MAP

I am trying to get back a count of records in the task / event object for contacts so that I can update a contact field with the correct vale of activity.

I am passing a LIST<ID> to the function. The trouble appears on the highlighted lines below
LIST<TASK> objTasks = [SELECT WHOID, count(ID) FROM TASK WHERE WHOID IN :ContactIDs];
LIST<EVENT> objEvents = [SELECT WHOID, count(ID) FROM EVENT WHERE WHOID IN :ContactIDs];

The Error I get is:
Field must be grouped or aggregated: WhoId

But if I try and group it, it gives me errors as well
LIST<TASK> objTasks = [SELECT WHOID, count(ID) FROM TASK WHERE WHOID IN :ContactIDs GROUP BY WHOID];
LIST<EVENT> objEvents = [SELECT WHOID, count(ID) FROM EVENT WHERE WHOID IN :ContactIDs GROUP BY WHOID];
This gives the error
Illegal assignment from List<AggregateResult> to List<Task>


 

The Function
PUBLIC VOID updateActivityCount( LIST<ID> listContacts )
	{
        LIST<ID> ContactIDs = NEW LIST<ID>();
		ContactIDs = listContacts;

		INTEGER tmpTasks = 0;
		INTEGER tmpEvents = 0;

		
		LIST<CONTACT> contactUpdate = NEW LIST<CONTACT>();
		MAP<ID, INTEGER> contactTasks = NEW MAP<ID, INTEGER>();
		MAP<ID, INTEGER> contactEvents = NEW MAP<ID, INTEGER>();

        LIST<TASK> objTasks = [SELECT WHOID, count(ID) FROM TASK WHERE WHOID IN :ContactIDs];
        LIST<EVENT> objEvents = [SELECT WHOID, count(ID) FROM EVENT WHERE WHOID IN :ContactIDs];

		FOR(TASK thisTask : objTasks)
		{	contactTasks.put(thisTask[0], thisTask[1]);	}

		FOR(EVENT thisEvent : objEvents)
		{	contactEvents.put(thisEvent[0], thisEvent[1]);	}

		LIST<CONTACT> objContacts = [SELECT ID FROM Contact WHERE ID IN :ContactIDs];
		FOR(CONTACT thisContact : objContacts)
		{
			IF( contactTasks.containsKey( thisContact.Id ) )
			{	tmpTasks = contactTasks.get( thisContact.Id );	}
			ELSE
			{	tmpTasks = 0;	}

			IF( contactEvents.containsKey( thisContact.Id ) )
			{	tmpTasks = contactEvents.get( thisContact.Id );	}
			ELSE
			{	tmpEvents = 0;	}

			thisContact.activity_this_cfy__c = tmpTasks + tmpEvents;
			contactUpdate.add( thisContact );
		}

		//--    Execute the update for contactUpdate
		//------------------------------------------------------------------------------------------------------------------------------
		if ( !contactUpdate.isEmpty() )
		{
			TRY{ update contactUpdate; }
			CATCH ( Exception ex )
			{   SYSTEM.DEBUG(thisPage + 'Could not update Contact. Cause: ' + ex.getCause() );  }
		}
	}	//--	END updateActivityCount


 
Michael DsozaMichael Dsoza
Hi Andrew,

You are using aggregate function (count) in your query that is why it is giving you an error for illegal assignment.
Use List<AggregateResult> and iterate it's result to prepare list of Task & Event respectively.

Mark it as best answer only if it helps you in best way.
Andrew TelfordAndrew Telford
Thanks Michael

That appears to have started me in the right direction ...
 
LIST<CONTACT> contactUpdate = NEW LIST<CONTACT>();
		MAP<ID, INTEGER> contactTasks = NEW MAP<ID, INTEGER>();
		MAP<ID, INTEGER> contactEvents = NEW MAP<ID, INTEGER>();

		AggregateResult[] objTasks = [SELECT WHOID, count(ID) FROM TASK WHERE WHOID IN :ContactIDs GROUP BY WHOID];
		AggregateResult[] objEvents = [SELECT WHOID, count(ID) FROM EVENT WHERE WHOID IN :ContactIDs GROUP BY WHOID];

		FOR(AggregateResult thisTask : objTasks)
		{	contactTasks.put( thisTask.get('WHOID'), thisTask.get('expr0'));	}

Still getting this erro though for the highlighted line
Incompatible key type Object for Map<Id,Integer>
I have tried changing the MAP ot <STRING, INTEGER> <STRING, DECIMAL> and <STRING, DOUBLE>
 
Andrew TelfordAndrew Telford
I have put the code in to the debug tool and narrowed the issue down to the WHOID. I can use system debug to print the whoid in the log file bt as soon as I try to UT the value into the MAP it give the error
 
16:04:07:745 USER_DEBUG [37]|DEBUG|0032000000XV3XUAA1
16:04:07:745 USER_DEBUG [38]|DEBUG|180
16:04:07:746 USER_DEBUG [37]|DEBUG|003200000087IFMAA2
16:04:07:746 USER_DEBUG [38]|DEBUG|1
Incompatible key type Object for Map<String,Double>
 
//--	-----------------------------------------------------------------
//--	updateActivityCount
//--	-----------------------------------------------------------------
		DOUBLE tmpTasks = 0;
		DOUBLE tmpEvents = 0;
        STRING tmpWHO = '';

		LIST<CONTACT> contactUpdate = NEW LIST<CONTACT>();
		MAP<STRING, DOUBLE> contactTasks = NEW MAP<STRING, DOUBLE>();

		AggregateResult[] objTasks = [SELECT WHOID, count(ID) FROM TASK WHERE ActivityDate >= 2016-08-28 GROUP BY WHOID];


		FOR(AggregateResult thisTask : objTasks)
		{
            system.debug(thisTask.get('WHOID')); 
            system.debug(thisTask.get('expr0'));
            contactTasks.put(thisTask.get('WHOID'), thisTask.get('expr0'));
        }