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
FinnArildFinnArild 

References and foreign keys

I have a trigger trying to access the name on the foreign key Contact:

 

 

trigger SAC_updateName on Support_Agreement_Contacts__c (before insert, before update) { for (Support_Agreement_Contacts__c sac:Trigger.new){ Contact[] c = [Select Name from Contact where Id =: sac.Contact_Id__c]; if (c.size() > 0) { sac.Name = c[0].Name; } else { sac.Name = 'John Doe'; } } }

 

Which obviously will generate a "Too many sql queries" on bulk updates, but it is like this because this doesn't work:

 

trigger SAC_updateName on Support_Agreement_Contacts__c (before insert, before update) { for (Support_Agreement_Contacts__c sac:Trigger.new){ if (sac.Contact_Id__c != null) { sac.Name = sac.Contact_Id__c.Name; } else { sac.Name = 'John Doe'; } } }

 

 

 

 

So - obviously, I don't get how this should work at all. I also tried the different __r and whatnots - but I could never get any real reference to the Contact object. Can anyone help a newbie out with this? 

Best Answer chosen by Admin (Salesforce Developers) 
Michael_KahleMichael_Kahle

Hi,

 

just select all Contacts you want to update in an extra for loop and put them into a Map<Id,Contact>.

You can then make allthe Adjustments you wish.

 

 

trigger SAC_updateName on Support_Agreement_Contacts__c (before insert, before update)
{
Set<Id> contactIdSet = new Set<Id>(); // holds the contact Ids off all contacts
Map<Id,Contact> contactMap; // binds the Id to the actual Contact

// Selecting All Ids of all contacts provided with the Support_Agreement_Contacts__c
for (Support_Agreement_Contacts__c sac:Trigger.new)
{
if(!contactIdSet.contains(sac.Contact_Id__c))
{
contactIdSet.add(sac.Contact_Id__c);
}
}

// Fill the Map with the actual Contacts
contactMap = new Map<Id,Contact>([SELECT Id,Name FROM Contact WHERE Id IN :contactIdSet]);

// Do the assignment in the custom Object based on the Map
for (Support_Agreement_Contacts__c sac:Trigger.new){
if (sac.Contact_Id__c != null) {
sac.Name = contactMap.get(sac.Contact_Id__c).Name;
} else {
sac.Name = 'John Doe';
}
}
}

 

This way you only have one SELECT call regardless how many Agreements are triggered.  Hope i helped out a bit.

 


 

 

Message Edited by Michael_Kahle on 20. 04. 2009 04:38 PM

All Answers

Michael_KahleMichael_Kahle

Hi,

 

just select all Contacts you want to update in an extra for loop and put them into a Map<Id,Contact>.

You can then make allthe Adjustments you wish.

 

 

trigger SAC_updateName on Support_Agreement_Contacts__c (before insert, before update)
{
Set<Id> contactIdSet = new Set<Id>(); // holds the contact Ids off all contacts
Map<Id,Contact> contactMap; // binds the Id to the actual Contact

// Selecting All Ids of all contacts provided with the Support_Agreement_Contacts__c
for (Support_Agreement_Contacts__c sac:Trigger.new)
{
if(!contactIdSet.contains(sac.Contact_Id__c))
{
contactIdSet.add(sac.Contact_Id__c);
}
}

// Fill the Map with the actual Contacts
contactMap = new Map<Id,Contact>([SELECT Id,Name FROM Contact WHERE Id IN :contactIdSet]);

// Do the assignment in the custom Object based on the Map
for (Support_Agreement_Contacts__c sac:Trigger.new){
if (sac.Contact_Id__c != null) {
sac.Name = contactMap.get(sac.Contact_Id__c).Name;
} else {
sac.Name = 'John Doe';
}
}
}

 

This way you only have one SELECT call regardless how many Agreements are triggered.  Hope i helped out a bit.

 


 

 

Message Edited by Michael_Kahle on 20. 04. 2009 04:38 PM
This was selected as the best answer
FinnArildFinnArild
A million thanks - this is a real eye opener - I see now how I can apply this to a lot of other stuff too.