1

I am trying to write a simple trigger that will populate field values on a newly created record.

I'm using standard objects: Account and Opportunity

I have two custom fields on the Account object called ChiefTechOwner__c and ComputerLocation__c. I've also added these fields on the Opportunity object.

What I need is when a new Opportunity record is created (and an Account Name for that opportunity is selected), I need a trigger to get the corresponding values for the ChiefTechOwner__c and ComputerLocation__c fields associated with that given Account, and populate it on the Opportunity record.

I've written the code below.
The "Before Insert" returns null because it is querying for ID's that are not yet created. I changed it to "After Insert", but then I get a "read-only" error.

If feel like it is a common scenario; can someone help me understand how to solve this problem?

trigger UpdOppty on Opportunity (before insert) {

Map<Id,Account> relatedAccounts = new Map<Id,Account>(
    [Select Id, ComputerLocation__c, ChiefTechOwner__c
        FROM Account
        WHERE Id 
        IN (Select AccountId 
                FROM Opportunity
                WHERE Id = :Trigger.new)
     ]
     );

for (Opportunity o : Trigger.new) {

 for (Account a : relatedAccounts.values()) {

   if (a.Id == o.AccountId) {

     o.ChiefTechOwner__c = a.ChiefTechOwner__c;
     o.ComputerLocation__c = a.ComputerLocation__c; 
   }
 }

} }

3

Your close, use a Map with the account ID from the Opportunity Records

trigger UpdOppty on Opportunity (before insert) {


    Map<Id,Account> relatedAccounts = new Map<Id,Account>();

    for (Opportunity o : Trigger.new) {
        relatedAccounts.put(o.AccountId,null);
    }

    relatedAccounts = New Map<ID,Account>([Select Id, ComputerLocation__c, ChiefTechOwner__c From Account Where Id In :relatedAccounts.keySet()]);

    for (Opportunity o : trigger.new) {
        Account a = relatedAccounts.get(o.AccountId);
        o.ChiefTechOwner__c = a.ChiefTechOwner__c;
        o.ComputerLocation__c = a.ComputerLocation__c; 

    }
}
  • This worked! Thank You! as a followup - what if these were two unrelated, custom objects? – Clink May 5 '16 at 4:38
  • If this solved your issue please mark it as accepted. If they were two unrelated objects how would you relate them? May be a topic for a new question – Eric May 5 '16 at 11:19
1

The above trigger will work when the opportunity is inserted. However, depending on the requirements, you may have more work to do. If the requirement is "keep the Chief Tech Owner and Computer Location" updated with what is on the account, then whenever those values are updated on the account, then the opportunity(ies) have to be updated too. If you're always having to show the latest value from the account, I'd recommend using formula fields instead to grab the account information.

If you're just stamping the opportunity with the Chief Tech Owner and Computer Location at the time the opportunity is created for snapshotting the info, then you're good.

The other thing to point out is that this could also be implemented through declarative means using process builder or even workflows. Whenever possible, use declarative means because they're usually faster to implement and you don't have to write test code for them, even though I recommend it anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.