+ Start a Discussion
noedskovnoedskov 

Using lookup fields with apex triggers

Hi,

 

I'm trying to create my first trigger. It seems pretty straight forward yet I seem to have a few problems/questions.

 

My trigger looks like this:

 

trigger createCase on Opportunity (after update) {
  for (Opportunity newOpp: Trigger.New) {
    if (newOpp.StageName == 'Order confirmed' && newOpp.Type == 'New Business') {
        Case newCase = new Case();
        System.debug('Account: ' + newOpp.Account);
        newCase.Account = newOpp.Account;
        insert newCase;
    }
  }
}

 

My 2 questions are:

 

  1. The new case is created but for some reason the account is not attached. Almost like the newOpp.Account is null. Which brings me to my next question...
  2. When testing the trigger in our sandbox I cannot see the debug message in the System Log. Why is that?

I'm not using the plug-in for Eclipse but simply writing the trigger directly in SFDC. I also tried running the trigger as "before update" but that didn't seem to make any difference in relation to the account not being attached to the case.

 

Cheers,

 

Søren Nødskov Hansen

Message Edited by noedskov on 02-13-2009 06:58 AM
noedskovnoedskov

Ok, sometimes you just need to write your questions down and then the answer presents itself to you :smileywink:

 

Instead of doing newCase.Account = newOpp.Account I tried newCase.AccountId = newOpp.AccountId which fixed the problem of the account not being attached to the case!

 

I've experienced the same thing with the API (Java) where I've tried to tie a lead together with a campaign member using myCampaignMember.setLead(myLead) but without any luck. The solution back then was actually exactly the same as now - using the IDs instead of the actual object.

 

Anyone know why that is?

 

Cheers,

 

Søren Nødskov Hansen