+ Start a Discussion
Nicholas MelonasNicholas Melonas 

Automatically Fill-In A Look-Up Relationship Field


On my custom object, "Projects," there is a field we manually fill in that connects it to the Opportunity object. However I also need a look-up relationship field that automatically populates the Account Name and connects the Project to the Account object. Where do I start and how can I do this? Is there a template formula I can use?

Hi Nicholas,

You cannot create formula field for this, because formula will not populate look up field. I have written trigger for this. I am assuming that you projects object has API name of Project__c and its has Opportunity__c as look up field. Now you will create one new look up field Account__c for Account. If whenever you populate opportunity, the trigger will populate Account. If your API names are different, please update in the code.
trigger ProjectTrg on Project__c( before update )
	Map<Id, Project__c> projects = new Map<Id, Project__c>([ Select Id, Opportynity__c, Opportunity__r.AccountId from Project__c where Id IN: trigger.new ]);
	for( Project__c proj : trigger.new )
		if( proj.Opportynity__c != null 
			&& oldMap.get( proj.Id ).Opportynity__c != proj.Opportynity__c
			&& projects.containsKey( proj.Id ))
			proj.Account__c = projects.get( proj.Id ).Opportunity__r.AccountId;
Let me know, if you need any other help.