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
Rakshith RamachandraRakshith Ramachandra 

When field A is updated, copy value from a field A of Object A to a field B in Object B.

Source Object - Activity
Source Field - FT_Emp__c
Destination Object - Account
Destination Field - Total_FTE__c

I want Total_FTE__c to be equal to FT_Emp__c when FT_Emp__c is updated. Here's the trigger I've come up with so far.
trigger Update_employee_count_in_activity on Task (after insert, after update) {
    // set up lists you will need
    List<Account> accsToUpdate = new List<Account>();    
    
    Map<Id, Task> taskMap = new Map<Id, Task>();

    // go through the list of tasks that were inserted
    for (Task t: Trigger.New)
    {
            taskMap.put(t.WhoId, Activity.FT_Emp__c);
    }
    
    if (taskMap.size() > 0)
    {
        // get all of the contacts related to the tasks
       accsToUpdate = [SELECT Id, Total_FTE__c
                        FROM Account 
                        WHERE Id IN: taskMap.keySet()];
        // go through the list for each contact
        for (Account c: accsToUpdate)
        {
            // total_fte_c should be equal to FT_Emp__c (Activity field)
            c.Total_FTE__c = taskMap.get(c.Id);
        }
        
        if (accsToUpdate.size() > 0)
        {
            update accsToUpdate;
        }
    }
}

The trigger doesn't seem to work and also I'm getting an error - "Save error: Entity is not api accessible". Any guidance would be appreciated. 

Thanks
Vishwajeet kumarVishwajeet kumar
Map<Id, Task> taskMap = new Map<Id, Task>(); ==> Looks suspisious to me.

taskMap.put(t.WhoId, Activity.FT_Emp__c); //key value doesn't seems to be matching to defined type. 

You should define the values of map same type as it is of field "Activity.FT_Emp__c".
Vishwajeet kumarVishwajeet kumar
Try this :

trigger Update_employee_count_in_activity on Task (after insert, after update) {
    // set up lists you will need
    List<Account> accsToUpdate = new List<Account>();    
    
    Map<Id, Task> taskMap = new Map<Id, Task>();

    // go through the list of tasks that were inserted
    for (Task t: Trigger.New)
    {
            taskMap.put(t.WhoId, t);
    }
    
    if (taskMap.size() > 0)
    {
        // get all of the contacts related to the tasks
       accsToUpdate = [SELECT Id, Total_FTE__c
                        FROM Account 
                        WHERE Id IN: taskMap.keySet()];
        // go through the list for each contact
        for (Account c: accsToUpdate)
        {
            // total_fte_c should be equal to FT_Emp__c (Activity field)
            c.Total_FTE__c = taskMap.get(c.Id).FT_Emp__c;
        }
        
        if (accsToUpdate.size() > 0)
        {
            update accsToUpdate;
        }
    }
}

this could work.
Rakshith RamachandraRakshith Ramachandra
Awesome. I don't see any errors. But I just tested the same but it's not updating the account record. I'm open to completely change the code for this trigger. It seems like a straight forward thing but I'm not able to figure it out as I'm new to apex coding.

Please help