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

Trigger not updating Contact.RelatedTo field, with no reason why not.


trigger UpdateReportsTo on User (after update) {
// user.Reports_to__c is a USER Id // contact ReportsTo is a CONTACT Id
 for (User u : Trigger.New) { if(u.Reports_to__c != null && u.Contact_Id__c != null){ // does the new Reports_to__c user have a contact id? User mgr = [Select Id, Contact_Id__c FROM User Where Id = :u.Reports_to__c]; if(mgr.Contact_Id__c != null){ // user has the contact linkage needed // update the target's contact record.ReportsTo with the mgr.Contact_Id__c value Contact targetmgr = [Select Id From Contact Where Id = :mgr.Contact_Id__c];   Contact userContact = new Contact(Id = u.Contact_Id__c); userContact.ReportsTo = targetMgr; update(userContact);  } } }}


The above trigger isn't firing both in unit tests as well as in the UI.  I get no errors...just no update to the contact record.  Any ideas at all?





Can you post your test ?


Sure, below is the test and under that are the methods in the TestHarness that this tet cares about.



private class ContactReportsToUserTest {
// grimes 2010-10-19
// tests the UpdateReportsTo  trigger

     private static testMethod void BaseAssertsOnContext(){
         TestContext tc = new TestContext();
         System.assertNotEquals(tc.targetContact, null);
         System.assertNotEquals(tc.targetContactMgr, null);
         System.assertNotEquals(tc.targetUser, null);
         System.assertNotEquals(tc.targetUserMgr, null);
         System.assertEquals(tc.targetContactMgr.Id, tc.targetContact.ReportsTo.Id);
         System.assertNotEquals(tc.targetUser.Contact_Id__c, null);
         System.assertNotEquals(tc.targetUserMgr.Contact_Id__c, null);
         //System.assertNotEquals(tc.targetUser.Reports_To__c, null);
        // System.assertNotEquals(tc.targetUserMgr.Reports_To__c, null);

     private static testMethod void Update_user_with_contact_id_not_valued_should_not_do_anything() {

     private static testMethod void Update_user_with_contact_id_valued_should_update_contact_record() {
         TestContext tc = new TestContext();
         System.assertEquals(tc.targetUser.Reports_To__c, null);
         // take the tc user and update the reports_to__c value to the manager User.  
         // assert that the User updated (not in the trigger, but for sanity     
         system.AssertEquals( tc.targetUserMgr.Id, tc.targetUser.Reports_to__c);    
         // assert that the user's contact record has the manager user's contact id value.
         system.AssertEquals(tc.targetContactMgr.Id, tc.targetContact.ReportsTo.Id);
     private virtual class TestContext {
         Contact targetContact;
         Contact targetContactMgr;
         User targetUser;
         User targetUserMgr;

        public TestContext () {
           // CreateMgrContactAndUserTHEN users
        private void CreateMgrContactAndUser(){
            // create the manager AND requery the subordinate for the mgr linkage.
            Contact mgr = TestHarness.CreateContactMgr();
            targetContactMgr = mgr;
            System.debug('CreateContactMgr targetContactMgr: ' + targetContactMgr);
            // create mgr user
            User u = TestHarness.CreateActualUser(targetContactMgr.Id, 'testmgr', '');
            targetUserMgr = u;
            System.debug('CreateContactMgr targetUserMgr : ' + targetUserMgr);
        private void CreateContactAndUser(){
            Contact t = TestHarness.CreateContactSubordinate(targetContactMgr);
            targetContact = t;
            System.debug('CreateContact targetContact: ' + targetContact);
            // create mgr user
            User u = TestHarness.CreateActualUser(targetContact.Id, 'testuser', '');
            targetUser= u;
            System.debug('CreateContact targetUser : ' + targetUser);
        public virtual void UpdateUserReportsTo(){
            System.debug('UpdateUserReportsTo targetUser.contact_Id__c' + targetUser.contact_Id__c);
            targetUser.Reports_to__c = targetUserMgr.Id;
        public virtual void ReSelectUser(){
            User u = [Select id, Reports_to__c, Contact_Id__c From User where Id = :targetUser.Id];
            targetUser = u;
        public virtual void ReSelectContact(){
            System.debug('ReSelectContact targetContact.Id' + targetContact.Id);
            Contact c = [Select ReportsTo.Id, Id From Contact Where Id = :targetContact.Id];
            targetContact = c;


public class TestHarness {

	public static Account CreateAccount(){
        User user = CreateUser();
        Account acct =  new Account(Name = 'Test Account', 
                            Test_Account__c = true, 
                            OwnerID = user.Id, 
        return acct;
    /* grimes 2010-10-19 */	
    public static Contact CreateContactSubordinate(Contact mgr){
        Contact c = new Contact(AccountId = mgr.Account.Id, FirstName = 'Test', LastName = 'User', ReportsTo = mgr);
        return c;
    public static Contact CreateContactMgr(){
      Account a = CreateAccount();
      Contact mgr = new Contact(AccountId = a.Id, FirstName = 'Test', LastName = 'User');
      return mgr;         
    public static User CreateActualUser(Id contactId, string username, string emailaddr){
          UserRole role = [Select Id From UserRole Limit 1][0];
          Profile profile = [Select Id From Profile Limit 1][0];
          Office__c office = [Select Id From Office__c Limit 1][0];
          User u = new User(LastName = 'Test'
                            , Alias = 'Test' 
                            , Email = emailaddr
                            , Username = emailaddr
                            , CommunityNickname = username
                            , UserRole = role
                            , ProfileId = profile.Id
                            , Office__c = office.Id
                            , TimeZoneSidKey = 'America/New_York'
                            , LocaleSidKey = 'en_US'
                            , EmailEncodingKey = 'ISO-8859-1'
                            , LanguageLocaleKey = 'en_US'
                            , Contact_Id__c = contactId
          return u;
    /* end */


I appreciate any help, I've been staring at this trivial trigger for hours which is more than frustrating.


- Bryan



Make sure that the trigger is active

It should fire. put some debug statements inside so you will come to know when it fires.

one suggestion, You put queries inside for loop which might hit the governor limit when you execute batch update on User object. you should devise you code so that this limitation can be avoided.



Make sure trigger is active

Put some debug logs and monitor  debug logs from SetUp--> Administrative SetUp==> Monitoring ==> Debug Log

If can view execution stack path


It looks inside your test Handler class ,

you have attemted to Mixed DML Exception,

First you have Insert user record and then Contact record , It will be Mixed DML ..





Thanks for the replies.  The trigger is active and valid, and I took out some of the System.debug statements in the snippets I posted before for brevity.  I'll look into inserting some more of those and using the System Log (redesigned apparently). 





Right, so as a follow up, I was able to get this to work...sort of.  I created a second field (text) in the contact table that I simply write the manager.Id to instead of writing the manager to the RelatedTo field.  No clue what the deal is, but that was it.  Now I have to see if that will satisfy workflow rules and the like, but it's something.


Anyone out there know if this is some known issue with the RelatedTo field on the Contact?


Oh, and when I was writing to the RelatedTo field I was testig by getting the SaveResult back and it was a Sucess each and every time, only with no vlaue change to the RelatedTo field.