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

Adding members to AccountTeamMembers in Trigger
I have this trigger below to pull out 4 users from my object Territories (ASD_Sales_Rep__c, PSD_Sales_Rep__c, Service_Sales_Rep__c, Switch_Sales_Rep__c, Transmission_Sales_Rep__c) and need to add those users in the AccountTeamMembers for the account. How do you add accountteammembers to an account? i dont see the account team members field for accounts so not sure where to place these.
trigger acctTeamUpdate on Account (after update, after insert) {
List<Account> accUpdateList = new List<Account>();
AccountTeamMember[] newmembers = new AccountTeamMember[]{};
for (Account a : Trigger.new)
{
Account acc = new Account();
Territory__c t;
try { // get the Territory record for the territory specified in the Account Record
t = [SELECT ID, Name, ASD_Sales_Rep__c, PSD_Sales_Rep__c, Service_Sales_Rep__c, Switch_Sales_Rep__c, Transmission_Sales_Rep__c
FROM Territory__c
WHERE ID=:a.Territory__c LIMIT 1];
}
catch(exception ex){
System.debug(' Failed Territory Record Retrieval');
}
AccountTeamMember addSalesRep=new AccountTeamMember();
addSalesRep.AccountId=a.id;
addSalesRep.UserId=t.ASD_Sales_Rep__c.uid;
newmembers.add(addSalesRep);
acc.Id = a.Id;
accUpdateList.add(acc);
}
try{
update accUpdateList;
}
catch(exception ex)
{
System.debug(' Failed Account Update');
}
}
trigger acctTeamUpdate on Account (after update, after insert) {
List<Account> accUpdateList = new List<Account>();
AccountTeamMember[] newmembers = new AccountTeamMember[]{};
for (Account a : Trigger.new)
{
Account acc = new Account();
Territory__c t;
try { // get the Territory record for the territory specified in the Account Record
t = [SELECT ID, Name, ASD_Sales_Rep__c, PSD_Sales_Rep__c, Service_Sales_Rep__c, Switch_Sales_Rep__c, Transmission_Sales_Rep__c
FROM Territory__c
WHERE ID=:a.Territory__c LIMIT 1];
}
catch(exception ex){
System.debug(' Failed Territory Record Retrieval');
}
AccountTeamMember addSalesRep=new AccountTeamMember();
addSalesRep.AccountId=a.id;
addSalesRep.UserId=t.ASD_Sales_Rep__c.uid;
newmembers.add(addSalesRep);
acc.Id = a.Id;
accUpdateList.add(acc);
}
try{
update accUpdateList;
}
catch(exception ex)
{
System.debug(' Failed Account Update');
}
}
The only possible reason for that error is any of the four fields having blank value.
But you have already checked that.
To be sure i have modified the code and put the null checks init.
Please try with the below trigger code. Let me know if you still get the error or not.
Thanks,
Abhishek
All Answers
I saw your trigger code and found that there are lots of things which need to be corrected.
I also figured out what you want to achieve so i have written a trigger as per your requirement.
Please find the trigger code below :
Please replace your trigger code with above and that will help you to acheive what you want.
Let me know if your requirements are other than this or if you have any issue in this.
Thanks,
Abhishek
I am getting the error: Error:Apex trigger acctTeamUpdate caused an unexpected exception, contact your administrator: acctTeamUpdate: execution of BeforeUpdate caused by: System.DmlException: Insert failed. First exception on row 3; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [User]: [User]: Trigger.acctTeamUpdate: line 24, column 1
it says missing field but i checked and all the users are populated in the territory. Any ideas?
The only possible reason for that error is any of the four fields having blank value.
But you have already checked that.
To be sure i have modified the code and put the null checks init.
Please try with the below trigger code. Let me know if you still get the error or not.
Thanks,
Abhishek