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
Abraham DurojaiyeAbraham Durojaiye 

How to create an Apex Trigger to auto-populate a custom field in an Opportunity object from a standard field in the same Opportunity object.

Hi,

I currently have an Apex Trigger that auto-poulates a custom field called Natural Partner in my Opportunity object. This field is auto-populated from an Account object.

Here is the current tirgger:

1. trigger NaturalPartner on Opportunity (before insert, before update) {
2.     map<id,account> accounts = new map<id,  account>();
3.     for (opportunity o:trigger.new) {
4.           accounts.put(o.accountid,null);
5.      }
6.      accounts.remove(null);
7.      accounts.putAll([Select id, name, Natural_Partner__c from account where id in :accounts.keyset()]);
8.      for(opportunity o:trigger.new) {
9.          if(accounts.containskey(o.accountid)) {
10.        
11.        o.Natural_Partner__c = accounts.get(o.accountid).Natural_Partner__c;
12.        
13.        
14.        }
15.    }
16. }

What I need is to change this trigger so that the custom field (Natural Partner) is auto-populated from the standard field (Opportunity Owner) which already auto-populates with the user's name that is making the entry. Both of these fields are in the Opportunity object.

Thanks
Gordon EngelGordon Engel
Why can't you use a formula field?
Abraham DurojaiyeAbraham Durojaiye
Hi Gordon,

What would the formula field be exactly? What formula would I use for this?