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
Jeff BloomerJeff Bloomer 

Update Lookup Field with Contents of Another Lookup Field in the Same Object

So, here's a newbie question (this is my very FIRST development project). I have a custom object called Account Corrections (Account_Correction__c), that we're using to document Account Owner change requests. On the form, there is a lookup field where the user looks up the Proposed Account Owner (Proposed_Account_Owner__c). Consequently, I want another lookup field to self-populate with the Proposed Account Owner's Manager (Proposed_Account_Owner_s_Manager__c). The reason I'm doing this is to facilitate an Approval Process on the backend that works best with lookup fields to dynamically specify approvers.

Anyway, I'm trying to use an "after insert" trigger to do the job , but I'm not clear on the approach to take. If someone could just put me on the right track, I would be VERY appreciative. If it helps, here are the fields from the Account Corrections object:
Status__c, Request_Details__c, Related_Account__c, Proposed_Account_Owner_s_Manager__c, Proposed_Account_Owner__c, OwnerId, Name, Id, Current_Acct_Owner_s_Manager__c, Current_Account_Owner__c, Assigned_To__c

 

The code below is as far as I've gotten:

trigger TRG_AccountCorrectionRequest_ChangeOwner on Account_Correction__c (after insert, after update) {

	for (Account_Correction__c ac: Trigger.new){
		if (ac.Status__c == 'Submitted' && ac.Request_Details__c == 'Assign Different Account Owner'){
			ac.Current_Acct_Owner_s_Manager__c = ac.Current_Account_Owner__r.ManagerId;
			ac.Proposed_Account_Owner_s_Manager__c = ac.Proposed_Account_Owner__r.ManagerId;
		}
	}
}

 Any assistance is very much appreciated.  Thanks in advance!

Best Answer chosen by Admin (Salesforce Developers) 
Jeff BloomerJeff Bloomer

Actually, I finally got this figured out.  Here is the code that ended up working.  Thanks for the assist by Vinit Kumar.

 

trigger TRG_AccountCorrectionRequest_ChangeOwner on Account_Correction__c (before insert, before update) {

    List<User> mgrId = new List<User>();
    Set<ID> setUserId = new set<ID>();
    
    for (Account_Correction__c ac: Trigger.new){
    	if (ac.Status__c == 'Submitted' && ac.Request_Details__c == 'Assign Different Account Owner'){
    		setUserId.add(ac.Proposed_Account_Owner__c);
    	}
    }
    
    mgrId = [Select ManagerId From User where id in: setUserId];

    for (Account_Correction__c ac: Trigger.new){
	    if (ac.Status__c == 'Submitted' && ac.Request_Details__c == 'Assign Different Account Owner'){
	    	ac.Proposed_Account_Owner_s_Manager__c = mgrId[0].ManagerId;
	    }
    }
}

 

All Answers

Shashikant SharmaShashikant Sharma

Hi Jeff,

 

Follow these steps

 

1. You need to have before trigger instead of after

2. As manager Id is field from User object so you could not fetch it from triiger context list (triger.new)

3. You have to create a set to contain the user ids and then query the user object

4. Have a map of user

 

Your code would be like below. It not exact but the idea how to achieve it.

 

To know more about trigger s you can see : http://forceschool.blogspot.in/2011/06/decimal-rounding-in-apex.html#comment-form

 

trigger TRG_AccountCorrectionRequest_ChangeOwner on Account_Correction__c (before insert, before update) {
Set<ID> setUserId = new set<ID>();

for (Account_Correction__c ac: Trigger.new){
    if (ac.Status__c == 'Submitted' && ac.Request_Details__c == 'Assign Different Account Owner')
   setUserId.add(ac.Proposed_Account_Owner__c);
}

Map<Id, User> mapUser = [Select MangerId  From User where id in: setUserId];

	for (Account_Correction__c ac: Trigger.new){
		if (ac.Status__c == 'Submitted' && ac.Request_Details__c == 'Assign Different Account Owner'){
			ac.Proposed_Account_Owner_s_Manager__c = mapUser.get(ac.Proposed_Account_Owner__c).ManagerId;
		}
	}
}

 let me know if you still face issues.

 

 


Jeff BloomerJeff Bloomer

Thanks for your quick reply on this.  I've been playing around with it, and evidently, I'm getting this error right now:

 

Save error: Illegal assignment from LIST<User> to MAP<Id,User>

 

Happening with this line of code:

Map<Id, User> mapUser = [Select MangerId  From User where id in: setUserId];
Jeff BloomerJeff Bloomer

Actually, I finally got this figured out.  Here is the code that ended up working.  Thanks for the assist by Vinit Kumar.

 

trigger TRG_AccountCorrectionRequest_ChangeOwner on Account_Correction__c (before insert, before update) {

    List<User> mgrId = new List<User>();
    Set<ID> setUserId = new set<ID>();
    
    for (Account_Correction__c ac: Trigger.new){
    	if (ac.Status__c == 'Submitted' && ac.Request_Details__c == 'Assign Different Account Owner'){
    		setUserId.add(ac.Proposed_Account_Owner__c);
    	}
    }
    
    mgrId = [Select ManagerId From User where id in: setUserId];

    for (Account_Correction__c ac: Trigger.new){
	    if (ac.Status__c == 'Submitted' && ac.Request_Details__c == 'Assign Different Account Owner'){
	    	ac.Proposed_Account_Owner_s_Manager__c = mgrId[0].ManagerId;
	    }
    }
}

 

This was selected as the best answer
Wiz76Wiz76

As an admin trying to learn enough about APEX to get by, this post really helped.

 

Thanks Jeff!

Jeff BloomerJeff Bloomer

Glad this could be of use.  I have since used this approach for other needs as well.  It's a nice little piece of code to recycle.