+ Start a Discussion

Overriding New button - Referencing the Opportunity's Account's

I'm trying to override the 'New' button for a custom object (Contract) tied to Opportunity.
When I click New I want it to populate the Client Name on the Contract with the name of the Account that the Opportunity is tied to, basically doing a two level field reference.
I've managed to set other fields, so that's not the problem. The problem is i don't know how to reference the Account Name.

To get the name of the Opportunity, I just use {!Opportunity.Name}, but you can't say {!Opportunity.Account.Name} or {!Account.Name} or something like that.
Do I need to do a SOQL query to get to the Account name or is it possible by any other notation?

Hello LohanLiljegren,

It depends on how you are accessing the data. If you are using the mail merge fields then you will want to utilize the Account Name on the Opportunity object. {!Opportunity.Account}. If you want the ID of the Account it would be the {!Opportunity.AccountId} I believe. If you are initiating it from the Opportunity you will need to utilize the Opportunity fields. If you want to have any fields from a related object you will be better off utilizing a query to pull that data.

var getAccount = sforce.connection.query("Select i.Account.Name From Opportunity i WHERE i.Id = '{!Opportunity.Id}'");

Or something similar. I don't have my development tools at work to verify that the query is correct but you should get the idea.
Thanks, I wasn't aware that there was both a {!Opportunity.Account} and a {!Opportunity.AccountId} merge field.
Will try it out as soon as I have a chance.