function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Tobias HaggeTobias Hagge 

Trigger lookup(relationship) from:parent to:children


I would like to get a lookup(relationship) field from an Account to all related Opportunities. I know I can access the Account field via formula field from the Opportunity, but then only get it as text value, where I would like to have it as lookup(relationship) type on the Opportunity as well.
Example: Have the Account Owner on all Opportunities. (not really a necessaty, only an example to understand what I would like)

Is there a how to what steps to follow on how to achieve this?

Thanks for the help in advance.
see if the below article is of any help
There is already a standard lookup on Opportunity to Account, if you want to get the Account fields on all the related opportunities in SOQL you can just use "Account." relationship. For example to get the OwnerId, 

List<Opportunity> acctOpportunities = [Select Id,Name, Account.OwnerId from Opportunity where Account.Name = 'Genepoint'];

See how I used Account.OwnerId & Account.Name in the SOQL. 
There's already a lookup relationship between the opportunity and its controllering account - its provided by the platform:

User-added image

You can add the account owner through a formula field - simply followw the relationship:

Tobias HaggeTobias Hagge
Not sure if I understand any of the three answers fully.

There is the same lookup(custom object) field on Accounts and Opportunities. Do I not need a trigger to get the value when set in Accounts to all Opportunities?
You don't need a trigger if you can use a formula field.  That way the value displayed in the opportunity is a reference to the actual value on the account record, so the next time you retrieve an opportunity from the database it will automatically contain the latest information.
Tobias HaggeTobias Hagge
As far as I understand I can only display lookup(relationship) fields through a formula in the following formats:


But not as a lookup(relationship) on the Opp, right?
Can you explaoin what you mean by "lookup (relationship) on the Opp". The lookup is the relationship, and you can travers the relationship to retrieve information from the related (account) object.  What do you want to do specifically with the relationship itself?
Tobias HaggeTobias Hagge
The lookup(relationship) on the Account is a lookup on a custom object. (like the Account Owner is a lookup on the User-object)
Now I would like the same lookup(relationship) field that references the custom object on the Opportunity, with the value equal to the value in the lookup(relationship) on the Account.
So you have an opportunity looking up to account looking up to a custom object?  That being the case, you can still do this in a formula field:


Assuming that the lookup field is named 'Custom_Object__c' (which means the relationship is named Custom_Object__r), you just follow the relationship to the account, then follow the relationship from the account to the custom object.  This works for up to 5 levels.
Tobias HaggeTobias Hagge
I don't think we quite understand each other yet.

User-added image

The field 'Sales Agent' is a lookup on the custom object 'Acquirer' on the Account. I know that I can query all information from the Opportunity and display it as text value. What I would like though is to have a field on the Opportunity which has the same functionality as the Sales Agent on the Account. with a link to the custom object. And whenever I change the Sales Agent on the Account, the Sales Agent also changes on all Opportunities.

I am pretty confident, that this wouldn't work with a formula field, would it?
Bindhyachal Kumar SinghBindhyachal Kumar Singh
Hi Tobias,

You can use follwoing formula field:

Formaula Type = text
Formula value = Account.Sales_Agent__r.Name

If you want a link like lookupfield then you have to write a trigger on Account and opportunity and when ever opportuntiy is inserted or account is update with changed sales agent then you can update sales Agent lookup field on opportunity.

Tobias HaggeTobias Hagge
Ok good. Now that we figured out on what the problem is, is there a step by step guide in how to write this specific trigger and what else would be to do?

F.e. do I need an apex class?
You'll definitely need an apex trigger, and and its best practice to use an apex class to actually carry out the processing.  There isn't a step by step guide for this type of trigger, just guides on trigger creation in general.  

You'll need to create a field that is a lookup to the Sales_Agent__c record and then I reckon you'll need  a couple of triggers:

1. A before insert trigger on opportunity, that copies the field from the account to the opportunity. This will need to be 'bulkified' and handle the situation where up to 200 records have been inserted, each of which could potentially have a different account parent.

2. An after update trigger on account, that updates all of the child opportunities when the field on the account changes. This will also have to be bulkified, and handle the situation where the field is changed on up to 200 records at once.