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
Darrell GallegosDarrell Gallegos 

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.

Tasks Creation from Multi-Select Picklist Values

Account Activities Created from Picklist
Best Answer chosen by Darrell Gallegos
cloudstreamercloudstreamer
trigger <triggerName> on Account (after insert){

//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

UC InnovationUC Innovation
Yes, the WhoId would work.  You would need to do something like:

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'.
Darrell GallegosDarrell Gallegos
Thants UC Innovation. I'm simply working in my developer org trying to teach myself APEX but I understand your point with the multiple contacts; therefore, with some help I was able to find the following solution. Thank you!

Multi Select Picklist Trigger to Task
 
UC InnovationUC Innovation
Since you are learning Apex...  You might want to read up on this:

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.
Darrell GallegosDarrell Gallegos
Thanks. Looks like I have to move it outside the initial for loop to query ALL Contacts related to the incoming Accounts. 
cloudstreamercloudstreamer
trigger <triggerName> on Account (after insert){

//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.
 
This was selected as the best answer
cloudstreamercloudstreamer
Please mark as best answer if this answers your question
Thanks
 
Darrell GallegosDarrell Gallegos
I can easily fix the insert. I guess that's why theyre called For loops. Ha-ha. Ill have to look at this code provided better to mKe sure I understand it. This is literally my fourth trigger. Thanks for all the Support 
Darrell GallegosDarrell Gallegos
I will have to think about all this information. If I move SOQL statement outside the For loop the myAccount variable is not defined. Same goes for the: insert listOfTasksToCreate.

As I said this is my fourth loop so, I still trying to understand all this.

Thank you.
Darrell GallegosDarrell Gallegos
Sorry I meant fourth trigger.