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

APEX Trigger Update Task Field from Contact Field
Trying to write (my first) trigger to pull from a picklist field on the Contact to a duplicate picklist on a Task field.
Contact field info is
Field Label: SOI
Object Name: Contact
Field Name: SOI
Data Type: Picklist
API Name: SOI__c
Task Field Info is
Field Label: Contact's SOI
Object Name: Activity
Field Name: Contacts_SOI
Data Type: Picklist
API Name: Contacts_SOI__c
I currently receive the error message:
Error: Compile Error: Incompatible key type Schema.SObjectField for MAP<Id,LIST<Task>> at line 6 column 20
The code that I am trying to use is:
Trigger TaskBefore on Task(before insert, before update){ Map<Id, List<Task>> whoIds = new Map<Id, List<Task>>{}; For (Task t : trigger.new) If(t.WhoId != null){ List<Task> tasks = whoIds.get(task.WhoId); If (tasks == null){ tasks = new List<Task>{}; whoIds.put(t.WhoId, tasks); } tasks.add(t); } For(Contact con : [Select Id, Name,SOI__c from Contact where Id in :whoIds.keySet()]) For(Task t : whoIds.get(con.id)) t.contacts_SOI__c = con.SOI__c;}
Please help!
final working code
Final working test
All Answers
Hi Brittanie,
I struggled to understand Maps my first few times writing triggers too. I've written the trigger you need to update all tasks related to a contact(WhoId) whith the value in SOI__c.
If you get any errors when saving check for misspellings. Hope this helps!
Recieveing
Error: Compile Error: unexpected token: for at line 26 column 4
Any ideas?
You have an extra curly bracket at line 23!
remove it and that shoudl do the trick.
Yes, I can see I missed a { after:
for(Task t :trigger.new){
Add that curly bracket and the code should compile fine.
Here is the complete code:
Noticed also had another error:
Map<Id, List<Task>> whoIdsMap = new Map<Id, List<Task>>{};
Map<Id, List<Task>> whoIdsMap = new Map<Id, List<Task>>();
Should be good now!
apparently I'm not ment to get this to work :(
Currently getting the following error but I swear these fields exist.
Error: Compile Error: Invalid field contacts_SOI__c for SObject Task at line 30 column 13
Hi Brittanie,
Look in your custom activity fields to make sure that the API name for the field is spelled correctly. If that is not the case then, I'm not sure what could be the problem.
Izay
final working code
Final working test