0

i am not sure why this error is shown,when i am mapping a new ID against and ID field.

Here i am trying to achieve this requirement : Want to do this for existing user. So i have queried the set of users [into list variable user}for which now I want to update the ManagerId with a new Id. Which you can see it in : for loop i am trying to update the set of users in the list with updated ManagerId and then adding it to TrsfrMgr list. But that assignment itself isnot working.

Can someone please help!!

List<User> FromMgrId ,ToMgrID ;
ToMgrID  = [select Id from User where name=: ToMgr] ;
for (User a : user)
          {
             system.debug('OLDMgrId' + a.ManagerId);
              a.ManagerId = (ID) ToMgrID.Id;
              TrsfrMgr.add(a);
          }
1
  • That is not even close to valid Apex. Please articulate more specifics about what you are trying to accomplish.
    – Adrian Larson
    Commented Jan 29, 2015 at 16:33

2 Answers 2

0

I think you are trying to assign one Id value to another. You should create a new user, initialize it with the ToMgrId.Id and then add it to the TrsfrMgr list.

2
  • hi @codeinprogress : yes but i want to do this for existing user. So i have queried the set of users [into list variable user}for which now I want to update the ManagerId with a new Id. WHich you can see it in : for loop i am trying to update the set of users in the list with updated ManagerId and then adding it to TrsfrMgr list. But that assignment itself isnot working.
    – user10727
    Commented Jan 29, 2015 at 16:46
  • Can you do something like this. Id tomgrId = [select Id from User where name=: ToMgr limit 1].Id ; and then in your for loop, a.ManagerId = tomgrId; Commented Jan 29, 2015 at 16:51
1

There error is in the following line

a.ManagerId = (ID) ToMgrID.Id;

The ToMgrID is a List and you are trying to access the ID field from it. You would need to get the user record from the list and then access the ID field.

2
  • yeah i know,but i was unable to figure out why it cant take ToMgrID.Id to assign it to another Id field. But now sorted with the resolution given by codeinprogress. :) bit strange though : as i understand ToMgrID = [select id user where name =:tomgr limit 1]. id is same as ToMgrID = [select id from user where name =: to mgr limit 1] then access Id by ToMgrID.id ,but that dint work :/
    – user10727
    Commented Jan 29, 2015 at 17:11
  • The reason is the difference in the data type. When you were retrieving using Name, you were defining ToMgrID as a List<User>. Whereas in the suggestion by codeinprogress, the ToMgrID was defined with data type as Id. In your previous code ToMgrID[0].Id would have worked as it would use the first record from the list.
    – Aayush K
    Commented Jan 29, 2015 at 17:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .