+ Start a Discussion
RamkumarVTRRamkumarVTR 

Update User Manager Field based on value of custom look up field

I have created a Custom lookup field called User_Manager__c in User , I need to update manager field in user based the the custom field. Please advise how to proceed

Best Answer chosen by RamkumarVTR
nathaForcenathaForce

Hi RamkumarVTR,

 

Is this a one-time change? As Satya_007 mentioned, the data loader or workbench can be useful to export/update the data.

 

If

- the number of Users to update is small and

- this is a one-time update, and

- you are planning on retiring the Test_Manager__c field,

 

a quick script would also be:

 

List<User> updatedUsers = new List<User>();
List<User> myUsers = [SELECT Id, User_Manager__c, Test_Manager__c
                      FROM User
                      WHERE isActive = true];

// looping through active users and checking if the target field is blank. If so, we will copy the Test_Manager__c value to it
for (User myUser: myUsers) {
  if (myUser.User_Manager__c == null) { // not sure if you want this condition
    myUser.User_Manager__c = myUser.Test_Manager__c;
    updatedUsers.add(myUser);
  }
}
update updatedUsers;

 and as digamberlucky mentioned, if you are planning on keeping the Test_Manager__c field, you should look into using a workflow for ongoing 'maintenance'.

 

Let us know if this helps

 

Nathalie

 

 

All Answers

Satyendra RawatSatyendra Rawat

hi,

fallow the bellow point.

1. If less than 50k then use the vf and apex code and set user id and update.

2. if more than 50k the use the batch apex and schedular.

3. using the apex data loader export the costom object data and set the user id and update the all record.

 

 

if you are facing any problem, feel free to reply me,

and enjoy.

 

digamber.prasaddigamber.prasad

Hi,

 

With Winter'14 release, you can create workflow on user object. Please try using that.

 

Let me know if you have any specific question.

RamkumarVTRRamkumarVTR

Actually , I need update the  Manager field in User object, based on the value of customised field called test_manager_c  from same user object, Please let me know if there is any code.

nathaForcenathaForce

Hi RamkumarVTR,

 

Is this a one-time change? As Satya_007 mentioned, the data loader or workbench can be useful to export/update the data.

 

If

- the number of Users to update is small and

- this is a one-time update, and

- you are planning on retiring the Test_Manager__c field,

 

a quick script would also be:

 

List<User> updatedUsers = new List<User>();
List<User> myUsers = [SELECT Id, User_Manager__c, Test_Manager__c
                      FROM User
                      WHERE isActive = true];

// looping through active users and checking if the target field is blank. If so, we will copy the Test_Manager__c value to it
for (User myUser: myUsers) {
  if (myUser.User_Manager__c == null) { // not sure if you want this condition
    myUser.User_Manager__c = myUser.Test_Manager__c;
    updatedUsers.add(myUser);
  }
}
update updatedUsers;

 and as digamberlucky mentioned, if you are planning on keeping the Test_Manager__c field, you should look into using a workflow for ongoing 'maintenance'.

 

Let us know if this helps

 

Nathalie

 

 

This was selected as the best answer
digamber.prasaddigamber.prasad

Have you already written some code for this? If yes, please share, will help you on top of it.

RamkumarVTRRamkumarVTR

Points to consider: 

 

We are planning to give access for the custom field only to the admin profiles, and they can update the managers when ever the user requested to update the field. 

 

1. Manager field is Standard field in user details 

2. Test Manager Field is custom field 

3. If i change the test manager field of a one user, it should reflect in in the manager field of the same user. 

 

 

nathaForcenathaForce

Hi RamkumarVTR,

 

sounds like you plan on using Test_manager__c. So as mentioned, use the script to update the current data (should absolutely do this in a sandbox first and verify it is what you want).

You should update the query to use the correct fields, and then update the for-loop:

 

Note that if the test_manager__c user is inactive it might create problems, so here is some updated code: 

 

List<User> updatedUsers = new List<User>();
Set<Id> managerIds = new Set<Id>();
List<User> myUsers = [SELECT Id, ManagerId, Test_Manager__c
                      FROM User
                      WHERE isActive = true];

// Gather Test_Manager__c ids so we can make a query to get the Active value for (User myUser: myUsers) { if (myUser.Test_Manager__c != null) { managerIds.add(myUser.Test_Manager__c); } } Map<Id, User> managers = new Map<Id, User>([SELECT Id, isActive FROM User WHERE Id in: managerIds]); // looping through active users and checking if the target field is blank. If so, we will copy the Test_Manager__c value to it for (User myUser: myUsers) { if ( managers.get(myUser.Test_Manager__c) != null && managers.get(myUser.Test_Manager__c).isActive == true) { myUser.ManagerId = myUser.Test_Manager__c; updatedUsers.add(myUser); } } update updatedUsers;

 

 

Digamberlucky, the workflow field update on user object does not allow for a formula on the manager field.

 

 

So you will need a trigger to perform this update. Here again, you might want to check if Test_Manager__c is the Id of an active user:

 

trigger UserTrigger on User(before insert, before update) {
  if (trigger.isInsert || trigger.isUpdate) { 
    for (User myUser: Trigger.New) {
      myUser.ManagerId = myUser.Test_Manager__c;
    }
  }
}

 

Please try in your sandbox and let us know if it helps