I have tried inserting a new record with ownerId and also with ownerName.But using "Ownername" shows the below error

  Account acc=new Account(Name='nEW',OwnerId='00528000001IpTb');
        insert acc;
        Account acc1=new Account(Name='nEW',Account__r.owner.firstname='gSS');
        insert acc;

Invalid field initializer: Account__r.owner.firstname

How can I use OwnerName instead of OwnerId?Or is there any work around?

  • When you first instantiate an account you can't set a related records field value like that. You would have to requery and change the field directly if I'm not mistaken Apr 21, 2016 at 14:54
  • 1
    To be honest, I'm not even sure what you are trying to do, can you clarify? Edit your post with more details Apr 21, 2016 at 14:57

1 Answer 1


Your problem is on this line

Account acc1=new Account(Name='nEW',Account__r.owner.firstname='gSS');

There is no relationship account__r.owner.firstname

The relationship is account.owner.somefieldonUser

and even so, when you insert an sobject, you can only instantiate fields directly on that sobject

so what you need is:

Account acc1=new Account(Name='nEW',ownerId = someExpressionThatderivesAnOwnerId);  

Once the Account has an ownerId, you can query the Account to discover things about the owner:

[select id, owner.firstname, owner.lastname, owner.alias from Account where someWhereExpression]
  • And if queue ownership is enabled?
    – Adrian Larson
    Apr 21, 2016 at 17:29
  • @adrianlarson can you have accounts owned by queues? success.salesforce.com/ideaview?id=08730000000BrESAA0
    – cropredy
    Apr 21, 2016 at 17:33
  • Woah. Didn't realize it's not an option. 7 years!? Such a simple request...
    – Adrian Larson
    Apr 21, 2016 at 17:51
  • @AdrianLarson (BTW - nice to see you back) in my experience, territories is a more robust way to handle multi-user 'ownership' of accounts so I can see why sfdc is dragging their heels
    – cropredy
    Apr 21, 2016 at 17:54
  • Gotcha, thanks! I just assumed polymorphism was at play. :)
    – Adrian Larson
    Apr 21, 2016 at 17:55

You must log in to answer this question.

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