You need to sign in to do that
Don't have an account?
Mahendiran Jayavarma 24
How to avoid SOQL inside for loop
Hi, I am getting error and seend SOQL Queries Inside FOR Loops. How to avaoid this.
private static void setOwnerToBrandAdmin(List<Contact> newContacts) {
List<Id> contactIds = new List<Id>();
for (Contact newContact : newContacts) {
contactIds.add(newContact.Id);
}
List<Contact> contacts = [SELECT Id, Brand__c, OwnerId, Owner.Name FROM Contact WHERE Id IN :contactIds];
List<Contact> contactsToUpdate = new List<Contact>();
for (Contact contact : contacts) {
if (contact.Owner.Name == 'Integration') {
List<Brand_Admin__c> admins = [SELECT User__c FROM Brand_Admin__c WHERE Brand__c = :contact.Brand__c];
if(admins.size() > 0) {
contact.OwnerId = admins[0].User__c;
contactsToUpdate.add(contact);
}
}
}
update contactsToUpdate;
}
private static void setOwnerToBrandAdmin(List<Contact> newContacts) {
List<Id> contactIds = new List<Id>();
for (Contact newContact : newContacts) {
contactIds.add(newContact.Id);
}
List<Contact> contacts = [SELECT Id, Brand__c, OwnerId, Owner.Name FROM Contact WHERE Id IN :contactIds];
List<Contact> contactsToUpdate = new List<Contact>();
for (Contact contact : contacts) {
if (contact.Owner.Name == 'Integration') {
List<Brand_Admin__c> admins = [SELECT User__c FROM Brand_Admin__c WHERE Brand__c = :contact.Brand__c];
if(admins.size() > 0) {
contact.OwnerId = admins[0].User__c;
contactsToUpdate.add(contact);
}
}
}
update contactsToUpdate;
}
Let me know if this gives you the same output