You need to sign in to do that
Don't have an account?
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?
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.
All Answers
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.