You need to sign in to do that
Don't have an account?

Apex To Many SQL Querys:21
Hello, I have read that the reason I get this error is due to my SOQL query being in the FOR loop of the below trigger. Could someone take a look at it and let me know how I could go about bulk safing this trigger.
trigger SetAccountField on Domains__c (before insert) {
for (Domains__c domain : Trigger.new) {
String cid = domain.Contact__c;
List<Contact> contacts = [SELECT AccountId,
MailingStreet,
MailingState,
MailingCity,
MailingPostalCode,
Company__c
FROM Contact WHERE Id = :cid];
if(contacts.size() > 0){
domain.Account__c = contacts.get(0).AccountId;
domain.Company__c = contacts.get(0).Company__c;
if(contacts.get(0).MailingStreet != null){
domain.House_Number__c = contacts.get(0).MailingStreet.substring(0,contacts.get(0).MailingStreet.indexOf(' '));
domain.Bill_To__c = contacts.get(0).MailingStreet.substring(contacts.get(0).MailingStreet.indexOf(' '));
}
domain.City__c = contacts.get(0).MailingCity;
domain.State_Province__c = contacts.get(0).MailingState;
domain.Zipcode__c = contacts.get(0).MailingPostalCode;
}
}
}
Any help is greatly appreciated.
Thanks,
Marc
try this
Set<Id> conIds = new Set<Id>();
for (Domains__c domain : Trigger.new) {
conIds.add(domain.Contact__c);
}
Map<Id, Contact> conMap = new Map<Id, Contact>([SELECT AccountId,
MailingStreet,
MailingState,
MailingCity,
MailingPostalCode,
Company__c
FROM Contact WHERE Id IN :conIds]);
for (Domains__c domain : Trigger.new) {
Contact current = conMap.get(domain.Contact__c);
domain.Account__c = current.AccountId;
domain.Company__c = current.Company__c;
etc.. try using ctach exceptions for null errors
}
All Answers
try this
Set<Id> conIds = new Set<Id>();
for (Domains__c domain : Trigger.new) {
conIds.add(domain.Contact__c);
}
Map<Id, Contact> conMap = new Map<Id, Contact>([SELECT AccountId,
MailingStreet,
MailingState,
MailingCity,
MailingPostalCode,
Company__c
FROM Contact WHERE Id IN :conIds]);
for (Domains__c domain : Trigger.new) {
Contact current = conMap.get(domain.Contact__c);
domain.Account__c = current.AccountId;
domain.Company__c = current.Company__c;
etc.. try using ctach exceptions for null errors
}
Thanks so much. My brain was just not grabbing the idea of what needed to be done.
Again, thanks.
A little help on the Catch statement might be a good thing. I am not the greates most knowledgable apex programmer. I seem to be getting null pointer exceptions. What would my catch need to look like in that case?
Thanks,
Marc
the contact on the domain object is that mandatory or can it be empty? if so you would probably put it there
something like
for (Domains__c domain : Trigger.new)
{
if(domain.contact__c == null)return;
conIds.add(domain.Contact__c);
}
for(Domain domain : trigger.new)
{
//if null stop everything else.
if(domain.contact__c == null)return;
Contact current = conMap.get(domain.contact__c);
try
{
//Do the rest here i.e. map fields
}
catch(NullPointerException e)
{
System.debug('### OMG NULL ERROR ###: ' + e.getMessage());
}
}
Thanks again. catch(system.nullpointerexception e) I had figured, I just wasn't sure what to do with it.
Thanks again for all of your help.