You need to sign in to do that
Don't have an account?
Retrieve the Account Related Contact Name to Populate New Task Trigger
Greetings All. First I am new to APEX but trying to grasp the funcitonality and coding language as best I can. I have included the lastest Trigger I have created. It successfully retrieves multiselect picklist values and creates new tasks related to the values that were selected and populates the activities related list on the Account detail page.
What I am trying to figure out, is how to populate the Related Contact name in the newly created task. I believe WhoId will work but not sure how to include this is the code to look across the Account record and retrieve the Related Contact. I hope I explained this well.
Any suggestions? I believe a SOQL statement would work but I am thinking the WHoId should as well.
//get all the Ids for the accounts that are inserted in that execution context.
Set<ID> ids = Trigger.newMap.keySet();
Map<Id,LIst<Contact>> accContactsMap = New Map<Id,LIst<Contact>>();
//populate the Map
for (Account acc:[SELECT Id, Name, (select ID from Contacts)
FROM Account where id = :ids]){
system.debug('Account'+acc);
system.debug('Account Contacts'+acc.Contacts);
if(acc.Contacts !=null && acc.Contacts.size()!=0){
accContactsMap.put(acc.Id,acc.Contacts);
}
system.debug('Account'+acc);
system.debug('Account Contacts'+acc.Contacts);
}
declare the listOfTasksToCreate variable
for(Account myAccount:Trigger.new){
//perform the logic
//build the task list
//retrieve the contact LIst for that account from the accContactsMap
//set the who id of the task . If there are more than 1 contact related to the account , you will have to either choose the first contact or choose appropriate contact to who you want to assign the task based on some logic.
}
//come out of the for loop and insert the listOfTasksToCreate
}//close the trigger
Note: In your code the insert listOfTasksToCreate is inside the for loop which is against best practice of coding in Apex.
All Answers
newTask.WhoId = <Contact ID>
However, how are you determining the Related Contact to the Task since an Account can have more than one Contact associated with it? If you know how to determine that, then just query for that Contact on the Account, and assign the Contact ID to ''newTask.WhoId'.
https://developer.salesforce.com/page/Apex_Code_Best_Practices
I see that you did a SOQL query within the first for loop, which will lead to SOQL query limits when there's a mass update on Accounts.
//get all the Ids for the accounts that are inserted in that execution context.
Set<ID> ids = Trigger.newMap.keySet();
Map<Id,LIst<Contact>> accContactsMap = New Map<Id,LIst<Contact>>();
//populate the Map
for (Account acc:[SELECT Id, Name, (select ID from Contacts)
FROM Account where id = :ids]){
system.debug('Account'+acc);
system.debug('Account Contacts'+acc.Contacts);
if(acc.Contacts !=null && acc.Contacts.size()!=0){
accContactsMap.put(acc.Id,acc.Contacts);
}
system.debug('Account'+acc);
system.debug('Account Contacts'+acc.Contacts);
}
declare the listOfTasksToCreate variable
for(Account myAccount:Trigger.new){
//perform the logic
//build the task list
//retrieve the contact LIst for that account from the accContactsMap
//set the who id of the task . If there are more than 1 contact related to the account , you will have to either choose the first contact or choose appropriate contact to who you want to assign the task based on some logic.
}
//come out of the for loop and insert the listOfTasksToCreate
}//close the trigger
Note: In your code the insert listOfTasksToCreate is inside the for loop which is against best practice of coding in Apex.
Thanks
As I said this is my fourth loop so, I still trying to understand all this.
Thank you.