function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Hanna BergsmaHanna Bergsma 

Help With Referencing Field From SOQL in Apex

I am working on a trigger that updates a field on an account when the formula field on the account hit 0 days remaining for the subscription. Some contacts are related to multiple accounts, so I need to look for Contact emails as well as contact emails via AccountContactRelations. 
I have the SOQL query for this. I now need to, at the end of my trigger, reference the contact.email field from the soql query. How do I do this?

 
trigger Renewal on Account (before update) { for(Account acc : Trigger.new) { if (acc.Active_Subscription__c = True && acc.Subscription_Days_Remaining__c = 0) { // Get a list of all records that are expiring List<Account> matchingRecords = [SELECT Id, Name, Subscription_Days_Remaining__c , (Select email FROM Contacts), (SELECT Contact.Email FROM AccountContactRelations) FROM Account WHERE Active = True and Subscription_Days_Remaining__c < 0 and Id = :acc.ID]; //update the account email to the contact email //update to relation email if role is forwarding contact, if that is null, contact email, add one to trigger field String matchingRecordDescription = 'Email this person: \n'; for (Account matchingRecord : matchingRecords){ matchingRecordDescription += matchingRecord.Contacts.Email; } } }
AbhinavAbhinav (Salesforce Developers) 
Hi Hanna,

Please share the Soql query which you are using.

Thanks!