You need to sign in to do that
Don't have an account?
Trigger on Contact cant recursively update itself - getting an error - need help
Hi All
I've been trying to get this run for a while now but cant get it. I'm not a fulltime apex programmer. I need help of all you veterans here in this discussion board.
I am trying to write a trigger on the Contact object that would fetch the values of one of the AccountTeamMember who has a profile 'P1'. To an extent, the debug values are fetching what I am trying to pull from the AccountTeamMember but am stuck in the last part of the code. I am getting the following error when I try to save the Contact record:
trigger SetContact_ASD_Life_User on Contact (before insert, before update) {
Id AccId = null;
Id uid = null;
Id cid = null;
List<Contact> con = trigger.old;
List<User> users;
for(integer i = 0; i<trigger.size;i++){
AccId = con[i].AccountId;
List<AccountTeamMember> atm = [SELECT UserId FROM AccountTeamMember where TeamMemberRole='ASD-Life' AND AccountId=:AccId limit 1];
if(atm!=null){
cid = con[i].Id;
System.Debug('Contact Id is '+cid);
AccountTeamMember m = atm[0];
System.Debug('MSG ASD Life User '+ m.UserId);
if(m.UserId!=null){uid = m.UserId;}
System.Debug('uid is : ' + uid);
users = [Select Username from User where Id=:m.UserId ];
}
}
Contact[] cont = [Select Id, ASD_Life_User3__c, Email from Contact where Id=:cid limit 1];
System.Debug('Contact Email is ' + cont[0].Email);
cont[0].ASD_Life_user3__c = uid;
update(cont);
}
Any suggestions how to fix this?
thanks
gm
Hi Grandmaster,
You should review how to bulkify your code. http://wiki.developerforce.com/page/Best_Practice%3A_Bulkify_Your_Code
Here is a revision of your code. I hope it works well for you.