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
grandmastergrandmaster 

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:

 

Apex trigger SetContact_ASD_Life_User caused an unexpected exception, contact your administrator: SetContact_ASD_Life_User: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 003E000000GvOz0IAF; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = 003E000000GvOz0) is currently in trigger SetContact_ASD_Life_User, therefore it cannot recursively update itself: []: Trigger.SetContact_ASD_Life_User: line 25, column 1

 
And here is the code:

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


Best Answer chosen by Admin (Salesforce Developers) 
Tim BarsottiTim Barsotti

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. 

 

trigger SetContact_ASD_Life_User on Contact (before insert, before update) {
Set<Id> AccountIDs = new Set<ID>();
    Map<Id, Id> AccountToTeamMemberMap = new Map<Id, Id>() //ID of Account, ID of TeamMember
for(Contact c: Trigger.new){ AccountIds.add(c.AccountId); //build a set of account IDS to query
}

//query for list of accounts List<AccountTeamMember> atmList = [SELECT UserId, AccountId FROM AccountTeamMember where TeamMemberRole='ASD-Life' AND AccountId in :AccountIDs];

//populate map from list
for(AccountTeamMember atm: atmList) {
AccountToTeamMemberMap.put(atm.AccountId, atm.UserId);
}

//populate field from map
for(Contact c: Trigger.new) {
if(c.AccountId != null) {
if(AccountToTeamMemberMap.get(c.AccountId) != null) {
System.Debug('Contact Id is '+c.id);
System.Debug('User ID: '+ AccountToTeamMemberMap.get(c.AccountId));
c.ASD_Life_user3__c = AccountToTeamMemberMap.get(c.AccountId);
}
}
} }