function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Andrew TelfordAndrew Telford 

Loop through sObject Map

I am attempting to use a frame work (http://chrisaldridge.com/triggers/lightweight-apex-trigger-framework/) and I am running in to an issue when I attempt to loop thorugh a MAP that is passed to a class.

This is the code I currently have
public void AfterInsert( Map<Id, SObject> newItems )
{
//– Collect the common information
//———————————————————————————————————————-
List<Contact> contsToUpdate = new List<Contact>();
List<Contact> updateContacts = new List<Contact>();
Set<<D> contactIDs = new Set<ID>();
List<Task> thisTask = new List<Task>();

SYSTEM.debug('Where Are We?: AfterInsert Task');
SYSTEM.debug('What type of object?: ' + newItems.getSObjectType() );
SYSTEM.debug('What is our Size: ' + newItems.size() );
SYSTEM.debug('Who?: ' + newItems.get('WhoId') );

Map<Id, Task> MyMap = new Map<Id, Task>(newItems); //– Error here 
thisTask.addAll(MyMap.values());
}

This is the error I get when I save the TaskTriggerHandler class
Invalid initial type Map<Id,SObject> for Map<Id,Task>

What am I missing?.
 
Sumit Kumar Singh 9Sumit Kumar Singh 9
Hi Andrew,
Try this one - 
public void AfterInsert( Map<Id, SObject> newItems )
{
//– Collect the common information
//———————————————————————————————————————-
List<Contact> contsToUpdate = new List<Contact>();
List<Contact> updateContacts = new List<Contact>();
Set<ID> contactIDs = new Set<ID>();
List<task> thisTask = new List<Task>();

SYSTEM.debug('Where Are We?: AfterInsert Task');
SYSTEM.debug('What type of object?: ' + newItems.getSObjectType() );
SYSTEM.debug('What is our Size: ' + newItems.size() );
SYSTEM.debug('Who?: ' + newItems.get('WhoId') );

Map<Id, SObject> MyMap = new Map<Id, SObject>(newItems); //– Error here 
for(SObject sObj : MyMap.values()) {
	thisTask.add((task)sObj);
}
Thanks,
Sumit Kumar Singh
Andrew TelfordAndrew Telford
Thanks Sumit

Doesn't appear to be liked in the Developer Console. Still getting an error relating to Signature

Code:
public class CI_Trigger_Task_Handler implements CI_TriggerHandler
{
    //--    Initiate additional Classes
    //----------------------------------------------------------------------------------------------------------------------
    CI_Class cic = new CI_Class();

    public Boolean IsDisabled()
    {
        return false;
    }

    public void BeforeInsert( List<SObject> newItems ) {}

    public void BeforeUpdate( Map<Id, SObject> newItems, Map<Id, SObject> oldItems ) {}

    public void BeforeDelete( Map<Id, SObject> oldItems ) {}

    public void AfterInsert( Map<Id, SObject> newItems ) 
    {
    //--    Collect the common information
    //----------------------------------------------------------------------------------------------------------------------
        List<Contact> contsToUpdate = new List<Contact>();
        List<Contact> updateContacts = new List<Contact>();
//        Set<LIST> contactIDs = new Set<LIST>();
//        List<Task> thisTask = new List<Task>();

        SYSTEM.debug('Where Are We?: AfterInsert Task');
        SYSTEM.debug('What type of object?: ' + newItems.getSObjectType() );
        SYSTEM.debug('What is our Size: ' + newItems.size() );

        Map<Id, SObject> myMap = new Map<Id, SObject>(newItems);
        FOR(SObject sObj : myMap.values() )
        {
			thisTask.add( task(sObj) ); 
// -- Error Here - Method does not exist or incorrect signature: task(SObject)
        }
        SYSTEM.debug('What is our Size of taskRecords: ' + taskRecords.size() );
		SYSTEM.debug('Let us See a value: ' + taskRecords.putAll() );

        /*


        FOR( Task tsk : thisTask )
        {
            contactIDs.add( tsk.WhoId );
        }

        SYSTEM.debug('DEBUG TASK: Number of Contacts: ' + contactIDs.size() );

        FOR ( Contact cont: contsToUpdate )
        {
            cont.activity_this_cfy__c = cic.get_ActivityCount( cont.Id );

    //--    Add changes to the updateContact object
    //--------------------------------------------------------------------------------------------------------------
            SYSTEM.debug('Adding the data to updateContact');
            updateContacts.add(cont);
        }
*/
    //--    Update the record if we need too
    //--------------------------------------------------------------------------------------------------------------
        if (!updateContacts.isEmpty()) 
        {
            try{ update updateContacts; }
            catch (Exception ex)
            { System.debug('Could not update Contact cause: ' + ex.getCause() + 'APEX TRIGGER: CI_TASK_AMP_CAMPAIGN'); }
        }

    }

    public void AfterUpdate( Map<Id, SObject> newItems, Map<Id, SObject> oldItems )
    {


    }

    public void AfterDelete( Map<Id, SObject> oldItems ) {}

    public void AfterUndelete( Map<Id, SObject> oldItems ) {}
}


Error:
Method does not exist or incorrect signature: task(SObject)
 
Sumit Kumar Singh 9Sumit Kumar Singh 9
Where have you defined the "taskRecords", its null. 
SYSTEM.debug('What is our Size of taskRecords: ' + thisTask.size());
SYSTEM.debug('Let us See a value: ' + thisTask);
Thnaks,
Sumit Kuamr Singh
 
Andrew TelfordAndrew Telford
taskRecords was what I had in there prevously but I replaced it with the code you suggested. I just haven't removed the code that came after your amendment. When I do remove it, there is no difference in the error message.