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
MariPMariP 

using relationship queries...

Hi !

 

I need some help...

 

In apex, I want to get email address from a contact linked to the account concerned by the request on which the trigger started :

 

I try to explain better :                                                                                                           Contact(Object)

                                                                                                                                                 Email

                                                                                                                                                 (I want to get this...)

 

                                                                       Account (Object)

                                                                       Field_service_manager_contact__c (lookup to contact)

 

Request__c (Object)                                                                                                           

Entity__c (lookup to Account)                                                                                             

(I am here)                                                                                                                             

 

Is it clear ?

I don't know how to code this in my class.....

 

Thanks a lot to help me !

Marie

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
MiddhaMiddha

Hi Marie, 

 

The query should look something like this:

 

 

Select Id, Entity__r.Field_service_manager_contact__r.Email from Request__c WHERE ......

 

You can check the relationship names by generating this query from APEX Explorer  or Eclipse.

 

All Answers

MiddhaMiddha

Hi Marie, 

 

The query should look something like this:

 

 

Select Id, Entity__r.Field_service_manager_contact__r.Email from Request__c WHERE ......

 

You can check the relationship names by generating this query from APEX Explorer  or Eclipse.

 

This was selected as the best answer
MariPMariP

Thanks a lot Gulshan,

I will follow your advice !

(and try to get this logic, really difficult for me !!!)

 

Marie

MariPMariP

I have a problem with the select.....

Her is my code :

 

 

public with sharing class CSR_CORE_RequestTriggers_SendSingleEmail {

// Objets
public Request__c oRequest {get;set;}
public Contact_external__c oContact_external {get;set;}
public Contact oContact {get;set;}

// Collections
public list<string> lTypesContactList {get;set;}
public list<string> lEmailAddressesList = new list<string>();
public list<string> lEmailOthersAddressesList = new list<string>();

public void SendEmail(List<Request__c> closedRequestList){
System.debug('## Start class ## CSR_CORE_RequestTriggers_SendSingleEmail - ligne 14 ' + UserInfo.getName());
	
	for (Request__c closedRequest:closedRequestList){

// GET EMAIL ADDRESSES
	
	Boolean resultTest;
	
	//put values of multi-select picklist field into a list
		list<string> lTypesContactList = closedRequest.Contact_types_for_Email__c.split(';', 15);
	
	// for every type of contact of the list, find its email address in Contact external
	     Integer count = 0 ;
	     for (String sTypesContact : lTypesContactList) {
	     	
	     Contact_external__c acc_Contact_External  = [select Name, Email__c from Contact_external__c 
		     where Account__c = :closedRequest.Entity__c 
		     and title__c = :sTypesContact limit 1 ];
	     	// it is an external contact
	     	 if(acc_Contact_External.Name != null){
	     	 	 // contact has an email address
				 if(acc_Contact_External.email__c != null){
				 	 // System.debug('ligne 39 / valeur de count = [' + count + ']');	
				     lEmailAddressesList.add(acc_Contact_External.email__c); 
				 }
			 // not found in external contact
		  		 } else {
		  		 	resultTest = sTypesContact.contains('laner');
		  		 	// is it a planer ?
		  		 	if(resultTest == true ) {
		  		 		Contact acc_Contact = [select Id, Entity__r.Field_service_manager_contact__r.Email 
		  		 		from Request__c where Id = closedRequest.Id];
		  		 	}
		  		 	//                                                     +
		  		 	System.debug('Contact external address = null');                                                                
		  		 	//}
		  		 }
	     }
 

 I'm sorry.....

 

MiddhaMiddha

Can you explain what problem are you facing, is it not saving? What is the error that is coming?

 

Have you checked your relationship names "__r", are these correct?

MariPMariP

Sorry, here is the error message I get when I want to compile :

 

unexpected token:  'closedRequest.Id'

MiddhaMiddha

Add a colon before that.

 

select Id, Entity__r.Field_service_manager_contact__r.Email 
		  		 		from Request__c where Id = :closedRequest.Id
MariPMariP

OK

I did that

 

After the message was :

Illegal assignment from LIST<Request__c> to SOBJECT:Contact

 

So I changed to  :

Request__c acc_Request = [select Id, Entity__r.Field_service_manager_contact__r.Email 
from Request__c where Id = :closedRequest.Id];

 

Request__c in place of Contact, and now it compiles !

 

Thanks a lot !

 

Could you explain to me how to test a query in Eclipse ? I don't know how to do that .....

Patrick DixonPatrick Dixon

From the package explorer, open the schema up in Eclipse, type your query into the Query Results box and click 'Run me'.