You need to sign in to do that
Don't have an account?

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
The Error I get is:
But if I try and group it, it gives me errors as well
The Function
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
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.
That appears to have started me in the right direction ...
Still getting this erro though for the highlighted line I have tried changing the MAP ot <STRING, INTEGER> <STRING, DECIMAL> and <STRING, DOUBLE>