Reputation: 7011
I have a trigger for insert on Opportunity:
trigger OpportunityInsertTrigger on Opportunity (before insert) {
System.debug('Triggered on insert of opportunity. Updating blockchain...');
for (Opportunity opp : Trigger.new) {
List<DNBCompany__c> 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');
}
}
I have a custom object called DNBCompany that is linked via a Lookup relation (DNBCompany__c) in Opportunity.
How can I retrieve the DNBCompany associated with opp?
[EDITED to reflect answer below]
Upvotes: 0
Views: 6340
Reputation: 3819
You could do something like this :
for (Opportunity opp : Trigger.new)
{
DNBCompany__c retrievedCompany = [SELECT Id, Name FROM DNBCompany__c WHERE Id = :opp.DNBCompany__c];
}
Since the lookup field will be the Id
of the company you want to retrieve, you can use an inline SOQL query to get the associated object back.
Keep in mind, you'll have to reference any additional fields you wish to process on in your SOQL query.
Upvotes: 2