title: How do I retrieve a related object using a Lookup field in Apex?
body: <p>I have a trigger for insert on Opportunity:</p>
  
  <pre><code>trigger OpportunityInsertTrigger on Opportunity (before insert) {
      System.debug('Triggered on insert of opportunity. Updating blockchain...');
      for (Opportunity opp : Trigger.new) {
          List&lt;DNBCompany__c&gt; retrievedCompany = [SELECT Id, Name, Duns__c FROM DNBCompany__c WHERE Id = :opp.DNBCompany__c];
          DNBCompany__c company = retrievedCompany.get(0);
          String duns = (String) company.get('Duns__c');            
      }
  }
  </code></pre>
  
  <p>I have a custom object called DNBCompany that is linked via a Lookup relation (DNBCompany__c) in Opportunity.</p>
  
  <p>How can I retrieve the DNBCompany associated with opp?</p>
  
  <p>[EDITED to reflect answer below]</p>