0

I have a Custom Object called Vehicle__c which has a Master Detail Relationship with Contact.(Vehicles owned by contacts); Another Custom Object Called Maintenance_Request__c which has a Master Detail relationship with Vehicle__c. How to Access the Email field of Contact Object using an instance of Maintenance_Request__c object?

On Trying the following code I am getting the error 'Variable does not exist: Vehicle__r.Contact__r'

trigger SendMaintenanceRequestMail on Maintenance_Request__c (after insert,after update) {
 List<String> emailList = new List<String>();
   List<Maintenance_Request__c> v = new List<Maintenance_Request__c>();
    v = [select Vehicle__r.Contact__r.Email from Maintenance_Request__c ];
    emailList.add(Vehicle__r.Contact__r.Email);
}

1 Answer 1

1

emailList.add(Vehicle__r.Contact__r.Email);

This line throws that error because there is no variable named Vehicle__r.
The first element before a dot should always be a variable or a class name (for static access) or a namespace.

In order to collect Contacts email you should loop over your list:

for (Maintenance_Request__c mr : [SELECT Vehicle__r.Contact__r.Email FROM Maintenance_Request__c]) {
    emailList.add(mr.Vehicle__r.Contact__r.Email);
}

By the way, there is no need of create a new instance of a list then replace it with a query result. A SOQL will always return an instance (will never return null).

2
  • Thanks for that Help. Feb 8 at 8:31
  • @DonalShijan The StackExchange culture involves upvoting helpful answers (which you'll need a bit more reputation score to be able to do) as well as accepting the most correct or most helpful answer. Accepting an answer gives you both reputation score, and also signals to the contributors that you don't need any more help with this question (and to the community at large that the information is likely to be correct/good)
    – Derek F
    Feb 8 at 21:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.