0

I am getting the following error

Compile Error: Illegal assignment from String to User at line 31 column 13

Here's my code :

Opportunity.Owner = Lead.Biz_Dev_Manager__c;

Here owner in opportunity is a lookup field and biz_dev_manager in lead is picklist(string) so it's throwing the above error where picklist(string) is not matching with user datatype. Can anyone tell me how to convert it and overcome this issue ?

2
  • is Biz_Dev_Manager__c contains the user's name? – Ratan Paul Feb 17 '17 at 12:58
  • Yes Ratan, biz_dev_manager__c picklist is having the user names x,y,z and I have to assign one of these to opportunity owner upon firing the trigger(new record creation). – Laxman Feb 17 '17 at 13:01
2

You need to query the User table based on the chosen User Name to get the Id for that user. The lookup field expects an Id.

Try something like this:

User bdmanager = [SELECT Id FROM User WHERE Name = :Lead.Biz_Dev_Manager__c LIMIT 1];

opportunity.OwnerId = bdmanager.Id;
update opportunity;
4
  • tried this one but at lead level we don't have feasibility to pull user ids. – Laxman Feb 17 '17 at 13:02
  • 1
    @Laxman, can you explain what you mean by "at lead level we don't have feasibility to pull user ids"? – Charles T Feb 17 '17 at 13:05
  • You still need to use the OwnerId field. – Adrian Larson Feb 17 '17 at 13:08
  • Good catch @AdrianLarson, careless copy pasting I guess ;) – Folkert Feb 17 '17 at 13:09
0

Here's a totally different way of approaching it now that picklist API values have been implemented...

For each picklist entry, set the manager's name as the label, but set their Salesforce User ID as the API name. In all display contexts you will see the label not the API name. But because the API name is the ID, you can obtain and assign it without doing an extra SOQL lookup.

The caveat is, now that your API names in the picklist are not the manager's name anymore, you have to ensure you're not at any point accidentally displaying the ID to the user instead of the name (this includes VisualForce, formula fields, reports, etc).

3
  • 1
    Interesting idea, but I'm not really a supporter of hard coding Id's. What happens if you create such a list with User Id's in a sandbox and then deploy it to production? – Folkert Feb 17 '17 at 13:27
  • @Folkert if the users already existed in production when you created the sandbox, the user IDs are the same. The converse scenario is, what happens if one of the users renames themselves? Then the search-for-name Apex code breaks. Really the ideal setup here is just a filtered User lookup. It's just unfortunate that the UX of looking up from among a small set of choices is significantly worse than with a picklist. – Charles T Feb 17 '17 at 14:52
  • Yea, unless a user is made in the sandbox and added to the picklist to test, and later recreated in production. In any case, I think we can agree that a picklist to select users isn't the right solution and needs a lot of maintenance :) – Folkert Feb 17 '17 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.