You can do something like this. Basically you would need to get the opportunities of all the records that you're inserting / updating, get the associated accounts and then set the account values in the customOject's account__c field. 

trigger autoPopulateTrigger on customObject__c (before insert, before update){

    //get the oppotyunity Id's and store it in a set.
    set&lt;Id&gt; opptyIdSet = new set&lt;Id&gt;();
    for(customObject__c co: trigger.new){
        if(co.opportuntiy__c != null){
            opptyIdSet.add(co.opportunity__c);
        }
    }

    //query the opportunity records and get the associated accounts.
    map&lt;id, opportunity&gt; opptyMap = new map&lt;id, opportunity&gt;{[SELECT id, accountid from opportunity where Id IN: opptyIdSet]};

    //update the account value based on the opportunity in the record.
    for(customObject__c co: trigger.new){
        if(opptyMap.containsKey(co.opportunity__c)){
            co.Account__c = opptyMap.get(co.opportunity__c).accountId;
        }       
    }

}


Trigger to autopopulate a lookup field via related record
I am trying to display the data from a REST API (URL: 'http://api.kivaws.org/v1/loans/38239/lenders.json') to display in a block. Below is my code. It is not working, any hint on why will be sincerely appreciated. 

/**
 * Implements hook_block_info().
 */
function restapiexample_block_info() {
  // This example comes from node.module.
  $blocks['kiva'] = array(
  'info' =&gt; t('Kiva Test Block'),
  'cache' =&gt; DRUPAL_NO_CACHE
);


return $blocks;
}

/**
 * Implements hook_block_view().
 */
function restapiexample_block_view($delta = '') {
// This example is adapted from node.module.
$block = array();

switch ($delta) {
  case 'kiva':
    $block['subject'] = t('Kiva Example');
    $block['content'] = array(
    '#theme'  =&gt;  'page',
    '#markup' =&gt;  kiva_list_loans(),
  break;
}
return $block;


}

function kiva_list_loans() {
  $request = drupal_http_request('http://api.kivaws.org/v1/loans/38239/lenders.json');
  return drupal_json_decode($request-&gt;data);
}


You can try the URL, good JSON data is being returned. Error I receive is several instances of the following text in the $messages area: "Warning: Cannot use a scalar value as an array in drupal_render() (line 5859 of /home/drupalpro/websites/example7.dev/includes/common.inc)"

UPDATE 1:
With help from comments/answer below, I made some progress. from my function "kiva_list_loans()", I am able to see the response in my $page array. I changed the '#theme' in my "$block['content']" in hook_block_view to 'page' - but still I only see the word "Array". How to get this to render?


