You need to sign in to do that
Don't have an account?
Brandon Nelson
Update multiple records using DML
I have Object 1 and Object 2. I have a field on Object 1 that looks at Object 2 and pulls that value from Object based on another field in Object 1.
When someone updates the field on Object 2, I need it to update ALL records on Object 1. I'm trying to write a trigger to do this, but I keep running into TOO MANY STATEMENTS error. PLEASE HELP!
When someone updates the field on Object 2, I need it to update ALL records on Object 1. I'm trying to write a trigger to do this, but I keep running into TOO MANY STATEMENTS error. PLEASE HELP!
trigger UpdatedTerritoryByState on Territory_By_State__c (after update) { String tbsState; String tbsTerritory; String updatedTerritory; String facID; //Setting the Territory By State vars for(Territory_By_State__c tbs : Trigger.new) { tbsState = tbs.State__c; tbsTerritory = tbs.Territory__c; } List<Facility__c> facilityToUpdate = new list<Facility__c>(); List<Facility__c> updateFacility = [SELECT Facility__c.id, Facility__c.physical_state__c FROM Facility__c WHERE Facility__c.physical_State__c = :tbsState]; for(Facility__c fac : updateFacility) { Facility__c f = new Facility__c(); f.Id = fac.Id; f.Territory__c = tbsTerritory; facilityToUpdate.add(f); } update facilityToUpdate; }
Try the following code it may be helpful for you:
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha
Same error:
UpdatedTerritoryByState: System.LimitException: Too many SOQL queries: 101
Please use the limit in SOQL query now updated limit in the following code.
Try the updated code it may be helpful for you:
trigger UpdatedTerritoryByState on Territory_By_State__c (after update) {
String tbsState;
String tbsTerritory;
for(Territory_By_State__c tbs : Trigger.new) {
if(tbs.State__c!=null){
tbsState = tbs.State__c;
}
if(tbs.Territory__c!=null){
tbsTerritory = tbs.Territory__c;
}
}
List<Facility__c> updateFacility =new List<Facility__c>();
updateFacility=[SELECT Id, Physical_state__c FROM Facility__c WHERE Physical_State__c = :tbsState LIMIT 10000];
if(updateFacility.size()>0)
{
for(Facility__c fac : updateFacility)
{
fac.Territory__c = tbsTerritory;
}
}
if(tbsTerritory!=null && updateFacility.size()>0)
{
update updateFacility;
}
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha