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
lp okllllp oklll 

Populating Contact on case which is related to account.

Hi,

I have 3 account types A B C

When on case account A is chosen B and C populate automatically. The trigger seems to work for that.

The problem is arising when I am trying to populate a contact related to account A

I simply do not know how to traverse the relationship.

This is my code

Trigger PopulateResellerCase on Case (before insert,before update) {   for (Case NewCase : Trigger.new)      If (NewCase.Accountid != null)    {      Account AccountReseller = [SELECT Id,Reseller__c,APA__c,(SELECT id,Contact.FirstName FROM Account.Contacts)FROM Account                              WHERE Id = :NewCase.Accountid                             ];                                  NewCase.APA__c = AccountReseller.APA__c;      
 NewCase.Reseller__c = AccountReseller.Reseller__c;                             
   }   }

I am administrator on the path of learning code. I have tried the following options below but always get an error.
NewCase.contact = AccountReseller.contact.id;
NewCase.contact = AccountReseller.account.contactid;

But nothing seems to work.






 
Best Answer chosen by lp oklll
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Well, in that case, try the code below:
 
trigger PopulateResellerCase on Case (before insert,before update) { 
	
	Set<Id> accountIds = new Set<Id>();
	
	for (Case cs : Trigger.new) {
		if (cs.AccountId != null)
			accountIds.add(cs.AccountId);
	}

	Map<Id, Account> accounts = new Map<Id, Account>();

	for(Account acc: [	SELECT 	Id, 
								Reseller__c, 
								APA__c, 
								(SELECT Id FROM Account.Contacts LIMIT 1)
						FROM Account WHERE Id = : accountIds]) {
		accounts.put(acc.Id, acc);
	}
						
	for (Case cs : Trigger.new) {
		cs.APA__c = accounts.get(cs.AccountId).APA__c;      
		cs.Reseller__c = accounts.get(cs.AccountId).Reseller__c;
		cs.ContactId = accounts.get(cs.AccountId).Contacts[0].Id;		
	}
}

Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.

All Answers

Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello,


Keep in mind that Account/Contact is an one to many relationship which means that you when you query (SELECT id,Contact.FirstName FROM Account.Contacts) it will return a list with zero, one or more contacts depending of how many are associated with that Account.

First of all, you have to understand the scenario and decides which Contact are going to associate with the Case? Can be anyone or you want to associate the first Contact created or do you want to get a Contact based if some criteria? 

Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
lp okllllp oklll
Hi Zuinglio

I will always have only one contact.

regards

lawrence

 
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Well, in that case, try the code below:
 
trigger PopulateResellerCase on Case (before insert,before update) { 
	
	Set<Id> accountIds = new Set<Id>();
	
	for (Case cs : Trigger.new) {
		if (cs.AccountId != null)
			accountIds.add(cs.AccountId);
	}

	Map<Id, Account> accounts = new Map<Id, Account>();

	for(Account acc: [	SELECT 	Id, 
								Reseller__c, 
								APA__c, 
								(SELECT Id FROM Account.Contacts LIMIT 1)
						FROM Account WHERE Id = : accountIds]) {
		accounts.put(acc.Id, acc);
	}
						
	for (Case cs : Trigger.new) {
		cs.APA__c = accounts.get(cs.AccountId).APA__c;      
		cs.Reseller__c = accounts.get(cs.AccountId).Reseller__c;
		cs.ContactId = accounts.get(cs.AccountId).Contacts[0].Id;		
	}
}

Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
This was selected as the best answer
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior

Hello,

In addition, have a look on this article: https://developer.salesforce.com/page/Apex_Code_Best_Practices  (https://developer.salesforce.com/page/Apex_Code_Best_Practices" target="_blank)

Practice the use of Map, List and Sets and it will be easier to know how to resolve and manage relationships.

Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
lp okllllp oklll
Thanks mate.

Its perfect.
 
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Glad to have helped. 

Please add this condition to prevent from getting an error when the Account has no Contacts:
 
if(!accounts.get(cs.AccountId).Contacts.isEmpty())
			cs.ContactId = accounts.get(cs.AccountId).Contacts[0].Id;