+ Start a Discussion

Trigger pulling from a lookup relationship

I created a lookup relationship between Opportunities and Accounts.  This is different from the default relationship, I'm using it for other internal purposes.

Anyway, I want to create a trigger that will fire when the Opportunity is created or updated and the relationship has been established.  In user terms, they click on the lookup icon and select the related account.

I have two problems which are probably related:

1:  How do I do the above in a test class for my trigger?
    If the lookup field (on Opportunity) is Partner_Account, do I set o.Partner_Account__c(r?)=<some account id>?

2: How do I refer to this relationship in the trigger for both the criteria and the updates?
    o.Partner_Account__r.Id !=null


1. you will have to create the Account before you test. Just like you can create any SObject in Apex.

Account partnerAccount = new Account(Name='test',and anyother fields you need.);
insert partnerAccount;
System.assertNotEquals(null, partnerAccount.id);

Then when you insert the Oppty, you use the partnerAccount.id in the custom lookup field.

2. you don't need to reference the __r in this case, because you are on the object you have the lookup on.
If the lookup field on your oppty is Partner Account

o.Partner_Account__c != null

Is this what you are talking about?