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
Mostafa Agourri 8Mostafa Agourri 8 

Account Team Member

Hello everyone , 

Im creating a trigger that automate creating a team member for an account if statement but its not working , there is my Code 

public static void CreateAccountTeam(List<Account> listAccounts){
        List<AccountTeamMember> LsTeamMember = new List <AccountTeamMember>();
        List<Account> LsAccs = new List<Account>();
        system.debug('Mostafa1'+ listAccounts);
            for (Account a :listAccounts)
            {
                system.debug('mostafa2a'+ a);
                    if(a.CreatedBy.Type_role__c == 'CAINV' || a.CreatedBy.Type_role__c == 'DESK')
                {
                  AccountTeamMember tm = new AccountTeamMember();
                  tm.UserId = a.CreatedById;
                  tm.AccountId = a.Id;
                  tm.TeamMemberRole='CAINV';
                    LsTeamMember.add(tm);
                  system.debug('Mostafa3'+LsTeamMember);
                }
        }
            insert LsTeamMember;

    }

 public void handleAfterInsert(List<Account> lstAccount){
        AP01_AccountRules.CreateAccountTeam(lstAccount);

    }

there is trigger : 
if(Trigger.isAfter && Trigger.isInsert){
        system.debug('## AccountTrigger -- after insert');
        handler.handleAfterInsert(Trigger.new);
    }

can some one help me to resolve this 
MichelCRMichelCR
Hi Mostafa,

A problem I'm seeing here is on the line where says "if(a.CreatedBy.Type_role__c == 'CAINV' || a.CreatedBy.Type_role__c == 'DESK')"
The context variables like Trigger.new only include one level of information in the records, I mean, for a.CreatedBy.Type_Role__c there is no coming information that deep, so that value will be null. You only will have access to a.CreatedById.

A possible solution could be to query all the accounts and bring that information before asking for it.

I hope this helps.
Mostafa Agourri 8Mostafa Agourri 8
Hello Michel i just test without the condition but its not working in insert but its working in update