[SalesForce] Inserting AccountTeamMember on Account

trigger AccountTeam on Account (after insert) {
       for(account acct : Trigger.new){
           if(acct.ParentId!=null && acct.VPA__c==false){
              Account Parentacct=[SELECT Id, OwnerId FROM Account WHERE Id=:acct.ParentId LIMIT 1];
              System.debug(Parentacct +'parentaccount');

              AccountTeamMember acctmem = new AccountTeamMember();
              acctmem.AccountId = acct.Id;
              System.debug(acct.Id + 'id');

              acctmem.UserId = Parentacct.OwnerId;
              acctmem.TeamMemberRole = 'Account Manager';
              System.debug(acctmem +'accounteam');
              insert acctmem;

The following error is thrown:

DML requires SObject or SObject list type: AccountTeamMember

Best Answer

I am adding this as answer as I wanted to help you more than a comment would allow. Your question has been downvoted as it shows a lack of effort on your part and is not clear what you are asking. Please read how to ask a question. More information would be helpful here, is that the full error message? No line number or class mentioned?

Now trying to help you...

I tried your trigger as is (minus the field VPA__c) in a dev org and it worked fine. The difference may be due to Account access settings, mine is public read/write. This example adds the respective AccountShare at the same time while creating the AccountTeamMember. I do not know this for a fact, just a suggestion.

The bad news / constructive feedback

You need to read up on Apex Best Practices. Your trigger:

  1. Has SOQL in a for loop (Why? governor limits)
  2. Has DML in a for loop (Why? governor limits)
  3. Has logic in the trigger itself (Why? lots of reasons!)
  4. It's not bulkified

Below is an example of a better approach. It is deliberately not complete.


trigger exAccountTrigger on Account (before insert, after insert,before update, after update) {

        //Before Trigger methods here



Helper Class

public without Sharing class exAccountHelper {
    public static void createTeamMember(List<Account> newAccs){
        List<AccountTeamMember> accMemLsit = new List<AccountTeamMember>();
        //Create AccountTeamMember logic here
        //Left as an exercise for the reader

        insert accMemLsit;
Related Topic