1

I have the following code

accountToUpdate = [select owner.name from Account where owner.name = :sGetOwnerOne];
accountToUpdate.owner.name = sGetOwnerTwo;
update accountToUpdate;

How can I update owner names? Compiler showed me the following error:

Field is not writeable: User.Name

I tried this:

Account a1 = [select owner.name from Account where owner.name = :sGetOwnerOne];
        a1.Owner.name  = sGetOwnerTwo;
        update a1;

But it didn't work

2
  • What exactly are you trying to accomplish? Are you trying to change the lookup to a User with that Name? You would need to find the User record Id first and set OwnerId based on that...
    – Adrian Larson
    Feb 9, 2016 at 20:57
  • I need to change the owner from one account to another. Feb 9, 2016 at 21:01

3 Answers 3

1
Account src = [SELECT Owner.Id FROM Account where Name = 'SOURCE COMPANY'];
Account dest = [SELECT Id, Owner.Id FROM Account where Name = 'DEST COMPANY'];
dest.OwnerId = src.OwnerId;
update dest;

Note that the 'Id' field in the dest SOQL query is key because Salesforce needs it to update that specific record.

Just try to replace company names with yours and you will see it happening.

I'm not sure what you have in mind but I would have done that using a list based on User objects instead of Accounts.

Also, your first SOQL statement can result in more than 1 records because you can have many accounts with a given owner.

5
  • Correct, that's excellent. But unfortunately this error is shown List has no rows for assignment to SObject why this happens? Thanks in advance. Feb 10, 2016 at 14:24
  • The message "List has no rows for assignment to SObject" shows up when the SOQL query does not return any object. I see you based your queries on owner 's name. It's a little weird because it should be the other way around but I'll try to translate the query the way you initially designed it.
    – OrizG
    Feb 10, 2016 at 18:24
  • List<Account> src = [SELECT Owner.Id FROM Account where Owner.Name = 'John']; List<Account> dest = [SELECT Id, Owner.Id FROM Account where Owner.Name = 'Mary']; if (src.size() > 0 && dest.size() > 0) { dest[0].OwnerId = src[0].OwnerId; } update dest; I'm not sure you want a mass change of ownership of Accounts or not. The APEX code above changes the owner of the dest Account but I find very unlikely that a user owns only one account.
    – OrizG
    Feb 10, 2016 at 19:48
  • Yes, it worked! :) Feb 10, 2016 at 20:45
  • Awesome! Can you mark as the right answer? Thanks!
    – OrizG
    Feb 11, 2016 at 0:11
6

If you want to change the Owner you can only do it by setting the OwnerId field. If you want to set it based on Name, you will need to query for the User record whose Id you wish to set.

List<User> candidates = [SELECT Id FROM User WHERE Name = :someName LIMIT 1];
if (!candidates.isEmpty()) someAccount.OwnerId = candidates[0].Id;

If I were building a selectList in a controller for this purpose, I would do as @sfdcfox recommends and map Id to Name.

public Id newOwnerId { get; set; }
public List<SelectOption> owners
{
    get
    {
        if (owners == null)
        {
            owners = new List<SelectOption>();
            for (AggregateResult aggregate : [
                SELECT OwnerId, Owner.Name FROM Account GROUP BY OwnerId, Owner.Name
            ]){
                SelectOption owner = new SelectOption(
                    aggregate.get('OwnerId'), aggregate.get('Owner.Name')
                );
                owners.add(owner);
            }
        }
    }
    private set;
}

Then in your markup:

<apex:selectList value="{!newOwnerId}">
    <apex:selectOptions value="{!owners}" />
</apex:selectList>

Note that you would need to set someAccount.OwnerId = newOwnerId to use the above approach.

6
  • In the selecteList only catches me the owner.name Feb 9, 2016 at 21:07
  • 1. That's not a complete sentence. 2. Your OP makes absolutely no mention of a selectList.
    – Adrian Larson
    Feb 9, 2016 at 21:08
  • Yes, it was because SOMEONE edited my post! >< Feb 9, 2016 at 21:09
  • 1
    @RodolfoLanshore I looked through the edit history, and there has never been any mention of a selectList in this question before now. You may have mentioned it in another question that you've posted, but we don't have that context in this question.
    – Derek F
    Feb 9, 2016 at 21:34
  • 1
    @RodolfoLanshore The point is you can't set Owner.Name, you need to set OwnerId.
    – Adrian Larson
    Feb 9, 2016 at 21:50
2

The problem is that you're trying to assign by name. Assuming you're building your select list from some sort of query, you should be able to do this:

SelectOption[] userOptions = new SelectOption[0];
for(User u:[SELECT Id, Name FROM User]) {
    userOptions.add(new SelectOption(u.Id, u.Name));
}
return userOptions;

This way, when the user selects a value, and it's assigned to sGetOwnerTwo, you'll have the user's Id value, which is how records are physically linked to each other. After that, you can then update your account's OwnerId value:

accountToUpdate.ownerId = sGetOwnerTwo;
update accountToUpdate;

Please note that if you're intending to query records, you should always try to query by ID values when possible. This gives better performance and ultimately makes sure you're selecting the right records (in case there's two John Does that work at your organization, you'll get the right John Doe).

3
  • But, the issue here is that I'm going to update is the ID of the user right? Feb 9, 2016 at 21:18
  • What I need to do is change only the owner of one account. At selecting for example [John Lennon - account X] in the first select list and [Michael Jordan - Account Y], at clicking the button merge, Account X will be Michael Jordan's Feb 9, 2016 at 21:21
  • @RodolfoLanshore If that's the case, you'll still want to store ID values in your select list values. Using names is fragile, and should not be used. It sounds like your code has other underlying problems that you need to address first.
    – sfdcfox
    Feb 9, 2016 at 22:06

You must log in to answer this question.

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