You need to sign in to do that
Don't have an account?

New to Apex -- need help with object references
I am writing Apex triggers, and attempting to grab information from existing objects. The challenge is in trying to obtain the information.
Two questions:
1) Accounts own Opportunities. Opportunities own a custom object called Revenue__c. Revenue needs to have a lookup relationship with Account, so that the Revenue related list will appear on the Account detail page. I've written a trigger to auto-populate the Account__c lookup field on a new revenue object when the revenue object is being saved, but I'm not getting the right phrasing to grab the Acount id.
I've tried:
List<Revenue__c> existingRev= [SELECT ID, Opportunity__c, Account__c FROM Revenue__c];
list<Revenue__c> listrev = new list<Revenue__c>();
for( Revenue__c rev1 : existingRev)
{
rev1.Account__c = rev1.Opportunity__r.AccountId ;
listrev.add(rev1);
}
update listrev;
But the assignment line isn't correct. Is this how you'd reference an object through another object?
2) In trying to grab data from a Text field and save it in the Phone field of the Contact, I'm getting Null stored there instead. Do I have to do something in order to get the text info to be accepted as a phone number?
Thanks.
NOTLSimpson:
Assuming your trigger is on Revenue__c -- the Trigger context will only be aware of Revenue__c fields, not any parent Opportunity fields. As such you need to fetch in a SOQL statement:
Set<ID> oIdSet = new Set<ID>();
for (Revenue__c rev: Trigger.new)
oIdSet.add(rev.opportunity__c);
Map<ID,Opportunity> oIdToOpportunityMap = new Map<ID,Opportunity> ([Select id, accountId from Opportunity where id IN: oIdSet]);
then, you'll need:
for (Revenue__c rev: Trigger.new)
rev.account__c = oIdToOpportunityMap.get(rev.opportunity__c);
and that's it; if this is in a before update/before insert trigger -- The Trigger context update of rev.account__c will be included in the database update. No update DML required
Please review SFDC APEX documentation on bulk trigger idioms and trigger contexts - all triggers should be written to handle a list of SObjects, never just one
All Answers
NOTLSimpson:
Assuming your trigger is on Revenue__c -- the Trigger context will only be aware of Revenue__c fields, not any parent Opportunity fields. As such you need to fetch in a SOQL statement:
Set<ID> oIdSet = new Set<ID>();
for (Revenue__c rev: Trigger.new)
oIdSet.add(rev.opportunity__c);
Map<ID,Opportunity> oIdToOpportunityMap = new Map<ID,Opportunity> ([Select id, accountId from Opportunity where id IN: oIdSet]);
then, you'll need:
for (Revenue__c rev: Trigger.new)
rev.account__c = oIdToOpportunityMap.get(rev.opportunity__c);
and that's it; if this is in a before update/before insert trigger -- The Trigger context update of rev.account__c will be included in the database update. No update DML required
Please review SFDC APEX documentation on bulk trigger idioms and trigger contexts - all triggers should be written to handle a list of SObjects, never just one
Thank you!