You need to sign in to do that
Don't have an account?
Is this trigger efficient ?
Hi All ,
Help for a new salesforce enthusiastic..Cance of improving this trigger for bulk loads? How can i use try catch?
Many Thanks!!
trigger updateGTAHotelCount on Account (after delete, after insert, after update) {
//The Triger will update the GTA Hotels Statistics for this year in Strategy Master City for the Accounts with Property Status = LIVE and RecordType= Property.
//The Account Object has lookup to City__c and city__c has a lookup to Pseudo_city__c object.
//This trigger will aggregate the total number of Accounts for a Master City, total number of Five star Account etc
public Set<id> cityIds = new Set<id>();
public Set<Id> mCityIds = new Set<Id>();
public Map<Id, Pseudo_City__c> MCitiesToUpdate = new Map<Id, Pseudo_City__c>();
public Map<Id,City__c> citiesToUpdate = new Map<Id,City__c>();
//Create a set of city Ids for the Accounts and Map of Cities to update
List<Pseudo_city__c> updateMasterCity = new List<Pseudo_city__c>();
if(trigger.isInsert || Trigger.isUpdate){
for(Account item : Trigger.new){
if (Item.recordTypeId=='012200000004rPU' && Item.Property_Status__c=='LIVE' )
cityIds.add(item.city_new__c);
}
system.debug('The list of cities for insert/Update-- ' + cityIds);
}
if(Trigger.isDelete){
for(Account item : Trigger.old){
if (Item.recordTypeId=='012200000004rPU' && Item.Property_Status__c=='LIVE' )
cityIds.add(item.city_new__c);
}
system.debug('The list of cities Delete -- ' + cityIds);
}
for(City__c Tempcity : [Select Master_City_Cluster__c from city__c where ID IN :cityIds ]){
mcityIds.add(Tempcity.Master_City_Cluster__c );
}
system.debug('The Master cities to be updated ' + mCityIds );
for(Pseudo_city__c mcity :[Select Id,GTA_3_Hotels__c,GTA_4_Hotels__c,GTA_5_Hotels__c,GTA_2_or_less_Hotels__c,GTA_Hotels_2012__c from Pseudo_City__c where Id in :mcityIds]){
MCitiesToUpdate.put(mcity.Id, mcity);
}
system.debug('The master City Details '+ MCitiesToUpdate );
for(City__c city :[Select Id, Name,Master_City_Cluster__c,City_Location__c,(Select Id,Name,Property_Status__c,Location__c,recordTypeId,Star_Rating__c,GTA_Connect_Chain__c,DI_Master_Chain__c,ThresholdC__c,Grand_Total_RNs_2013__c from city__c.Accounts1__r) FROM city__c where Master_City_Cluster__c IN :mcityIds]){
If(city.Master_City_Cluster__c != null )
citiesToUpdate.put(city.Id,city);
}
system.debug('The cities to be updated ' + citiesToUpdate);
//Loop through every mastercity, then cities for each master cities and then accounts for each city and count them total and then based on matching criteria
for(Pseudo_city__c MCToUpdate :MCitiesToUpdate.values()){
//Initialize the variable to 0
integer mtotalLiveAccount = 0;
integer mtotalfivestarHotels=0;
integer mtotalfourstarHotels =0;
integer mtotalthreestarHotels =0;
integer mTotalGTAconnectRegional = 0;
integer mTotalGTAconnectGlobal = 0;
integer mTotalGTAconnectIndependent = 0;
integer mtotalCityCenterAccount = 0;
integer mtotalAirportAccount=0;
integer mtotalRegionAccount =0;
integer mtotalPOIAccount=0;
integer mtotalTwoStarorLess=0;
double GrandTotalRNs2013=0;
//looping thru the Master cities
for(city__c citi : citiesToUpdate.values()){
//looping through the cities in this Master city
if(citi.City_Location__c == 'City Center' && citi.City_Location__c != null ) {
for(Account acc : citi.Accounts1__r){
If(acc.Property_Status__c=='LIVE' && acc.recordTypeId=='012200000004rPU')
mtotalCityCenterAccount+= 1;
}
system.debug('In city loop City Center Hotels : ' + mtotalCityCenterAccount);
}
if(citi.City_Location__c == 'Suburb & Airport' && citi.City_Location__c != null) {
for(Account acc : citi.Accounts1__r){
If(acc.Property_Status__c=='LIVE' && acc.recordTypeId=='012200000004rPU')
mtotalAirportAccount+= 1;
}
system.debug('In city loop Airport Hotels : ' + mtotalAirportAccount);
}
if(citi.City_Location__c == 'Point of interest' && citi.City_Location__c != null) {
for(Account acc : citi.Accounts1__r){
If(acc.Property_Status__c=='LIVE' && acc.recordTypeId=='012200000004rPU')
mtotalPOIAccount+= 1;
}
system.debug('In city loop POI Hotels : ' + mtotalPOIAccount);
}
for(Account acc : citi.Accounts1__r){
If(acc.Property_Status__c=='LIVE' && acc.recordTypeId=='012200000004rPU')
mtotalLiveAccount+= 1;
if (acc.Star_Rating__c=='5 Star' && acc.Star_Rating__c != null)
If(acc.Property_Status__c=='LIVE' && acc.recordTypeId=='012200000004rPU')
mtotalfivestarHotels += 1;
if (acc.Star_Rating__c=='4 Star' && acc.Star_Rating__c != null)
If(acc.Property_Status__c=='LIVE' && acc.recordTypeId=='012200000004rPU')
mtotalfourstarHotels += 1;
if (acc.Star_Rating__c=='3 Star' && acc.Star_Rating__c != null )
If(acc.Property_Status__c=='LIVE' && acc.recordTypeId=='012200000004rPU')
mtotalthreestarHotels += 1;
if(acc.Star_Rating__c =='1 Star' || acc.Star_Rating__c=='2 Star' || acc.Star_Rating__c== 'NA')
If(acc.Property_Status__c=='LIVE' && acc.recordTypeId=='012200000004rPU')
mtotalTwoStarorLess+=1;
// if (((acc.DI_Master_Chain__c != 'ACCOR') || (acc.DI_Master_Chain__c != 'BW') || (acc.DI_Master_Chain__c != 'DESIGN') || (acc.DI_Master_Chain__c != 'EC') || (acc.DI_Master_Chain__c != 'FAIRMONT') || (acc.DI_Master_Chain__c != 'HILTON') || (acc.DI_Master_Chain__c != 'HYATT') || (acc.DI_Master_Chain__c != 'IHG') || (acc.DI_Master_Chain__c != 'JUMEIRAH') || (acc.DI_Master_Chain__c != 'KI') || (acc.DI_Master_Chain__c != 'LUXE') || (acc.DI_Master_Chain__c != 'MC') || (acc.DI_Master_Chain__c != 'MK') || (acc.DI_Master_Chain__c != 'MOHG') || (acc.DI_Master_Chain__c != 'SG') || (acc.DI_Master_Chain__c != 'WY') ) && (acc.GTA_Connect_Chain__c != null) && ((acc.GTA_Connect_Chain__c != 'Intercontinental Hotels Group') || (acc.GTA_Connect_Chain__c != 'Accor Hotels') || (acc.GTA_Connect_Chain__c != 'Best Western International') || (acc.GTA_Connect_Chain__c != 'Carlson Hospitality') || (acc.GTA_Connect_Chain__c != 'Choice Hotels International') || (acc.GTA_Connect_Chain__c != 'Design Hotels') || (acc.GTA_Connect_Chain__c != 'Fairmont Raffles Swissotel Hotels & Resorts') || (acc.GTA_Connect_Chain__c != 'Four Seasons Hotels & Resorts') || (acc.GTA_Connect_Chain__c != 'Hilton Worldwide') || (acc.GTA_Connect_Chain__c != 'Hyatt Hotels and Resorts') || (acc.GTA_Connect_Chain__c != 'Jumeirah International LLC') || (acc.GTA_Connect_Chain__c != 'Kempinski') || (acc.GTA_Connect_Chain__c != 'KEMPINSKI HOTELS & RESORTS') || (acc.GTA_Connect_Chain__c != 'Langham Hotels') || (acc.GTA_Connect_Chain__c != 'Luxe Worldwide Hotels') || (acc.GTA_Connect_Chain__c != 'Mandarin Oriental Hotel Group') || (acc.GTA_Connect_Chain__c != 'Marriott International Inc.') || (acc.GTA_Connect_Chain__c != 'Millennium & Copthorne Hotels plc') || (acc.GTA_Connect_Chain__c != 'Moevenpick Hotels & Resorts') || (acc.GTA_Connect_Chain__c != 'Shangri-La Hotels and Resorts') || (acc.GTA_Connect_Chain__c != 'Starwood Hotels and Resorts') || (acc.GTA_Connect_Chain__c != 'The Ascott Group') || (acc.GTA_Connect_Chain__c != 'Wyndham Hotel Group' ) || (acc.GTA_Connect_Chain__c != 'independent' )))
// If(acc.Property_Status__c=='LIVE' && acc.recordTypeId=='012200000004rPU')
// mTotalGTAconnectRegional+= 1;
if (!((acc.DI_Master_Chain__c == 'ACCOR') || (acc.DI_Master_Chain__c == 'BW') || (acc.DI_Master_Chain__c == 'DESIGN') || (acc.DI_Master_Chain__c == 'EC') || (acc.DI_Master_Chain__c == 'FAIRMONT') || (acc.DI_Master_Chain__c == 'HILTON') || (acc.DI_Master_Chain__c == 'HYATT') || (acc.DI_Master_Chain__c == 'IHG') || (acc.DI_Master_Chain__c == 'JUMEIRAH') || (acc.DI_Master_Chain__c == 'KI') || (acc.DI_Master_Chain__c == 'LUXE') || (acc.DI_Master_Chain__c == 'MC') || (acc.DI_Master_Chain__c == 'MK') || (acc.DI_Master_Chain__c == 'MOHG') || (acc.DI_Master_Chain__c == 'SG') || (acc.DI_Master_Chain__c == 'WY') ) && (acc.GTA_Connect_Chain__c != null) && !((acc.GTA_Connect_Chain__c == 'Intercontinental Hotels Group') || (acc.GTA_Connect_Chain__c == 'Accor Hotels') || (acc.GTA_Connect_Chain__c == 'Best Western International') || (acc.GTA_Connect_Chain__c == 'Carlson Hospitality') || (acc.GTA_Connect_Chain__c == 'Choice Hotels International') || (acc.GTA_Connect_Chain__c == 'Design Hotels') || (acc.GTA_Connect_Chain__c == 'Fairmont Raffles Swissotel Hotels & Resorts') || (acc.GTA_Connect_Chain__c == 'Four Seasons Hotels & Resorts') || (acc.GTA_Connect_Chain__c == 'Hilton Worldwide') || (acc.GTA_Connect_Chain__c == 'Hyatt Hotels and Resorts') || (acc.GTA_Connect_Chain__c == 'Jumeirah International LLC') || (acc.GTA_Connect_Chain__c == 'Kempinski') || (acc.GTA_Connect_Chain__c == 'KEMPINSKI HOTELS & RESORTS') || (acc.GTA_Connect_Chain__c == 'Langham Hotels') || (acc.GTA_Connect_Chain__c == 'Luxe Worldwide Hotels') || (acc.GTA_Connect_Chain__c == 'Mandarin Oriental Hotel Group') || (acc.GTA_Connect_Chain__c == 'Marriott International Inc.') || (acc.GTA_Connect_Chain__c == 'Millennium & Copthorne Hotels plc') || (acc.GTA_Connect_Chain__c == 'Moevenpick Hotels & Resorts') || (acc.GTA_Connect_Chain__c == 'Shangri-La Hotels and Resorts') || (acc.GTA_Connect_Chain__c == 'Starwood Hotels and Resorts') || (acc.GTA_Connect_Chain__c == 'The Ascott Group') || (acc.GTA_Connect_Chain__c == 'Wyndham Hotel Group' ) || (acc.GTA_Connect_Chain__c == 'independent' )))
If(acc.Property_Status__c=='LIVE' && acc.recordTypeId=='012200000004rPU')
mTotalGTAconnectRegional+= 1;
if (((acc.GTA_Connect_Chain__c == 'Intercontinental Hotels Group') || (acc.GTA_Connect_Chain__c == 'Accor Hotels') || (acc.GTA_Connect_Chain__c == 'Best Western International') || (acc.GTA_Connect_Chain__c == 'Carlson Hospitality') || (acc.GTA_Connect_Chain__c == 'Choice Hotels International') || (acc.GTA_Connect_Chain__c == 'Fairmont Raffles Swissotel Hotels & Resorts') || (acc.GTA_Connect_Chain__c == 'Four Seasons Hotels & Resorts') || (acc.GTA_Connect_Chain__c == 'Hilton Worldwide') || (acc.GTA_Connect_Chain__c == 'Hyatt Hotels and Resorts') || (acc.GTA_Connect_Chain__c == 'Jumeirah International LLC') || (acc.GTA_Connect_Chain__c == 'Kempinski') || (acc.GTA_Connect_Chain__c == 'KEMPINSKI HOTELS & RESORTS') || (acc.GTA_Connect_Chain__c == 'Langham Hotels') || (acc.GTA_Connect_Chain__c == 'Luxe Worldwide Hotels') || (acc.GTA_Connect_Chain__c == 'Mandarin Oriental Hotel Group') || (acc.GTA_Connect_Chain__c == 'Marriott International Inc.') || (acc.GTA_Connect_Chain__c == 'Millennium & Copthorne Hotels plc') || (acc.GTA_Connect_Chain__c == 'Moevenpick Hotels & Resorts') || (acc.GTA_Connect_Chain__c == 'Shangri-La Hotels and Resorts') || (acc.GTA_Connect_Chain__c == 'Starwood Hotels and Resorts') || (acc.GTA_Connect_Chain__c == 'The Ascott Group') || (acc.GTA_Connect_Chain__c == 'Wyndham Hotel Group')) && !(acc.GTA_Connect_Chain__c == null))
If(acc.Property_Status__c=='LIVE' && acc.recordTypeId=='012200000004rPU')
mTotalGTAconnectGlobal+= 1;
if(((acc.GTA_Connect_Chain__c == 'independent') && !((acc.DI_Master_Chain__c == 'ACCOR') || (acc.DI_Master_Chain__c == 'BW') || (acc.DI_Master_Chain__c == 'EC') || (acc.DI_Master_Chain__c == 'FAIRMONT') || (acc.DI_Master_Chain__c == 'HILTON') || (acc.DI_Master_Chain__c == 'HYATT') || (acc.DI_Master_Chain__c == 'IHG') || (acc.DI_Master_Chain__c == 'JUMEIRAH') || (acc.DI_Master_Chain__c == 'KI') || (acc.DI_Master_Chain__c == 'MC') || (acc.DI_Master_Chain__c == 'MK') || (acc.DI_Master_Chain__c == 'MOHG') || (acc.DI_Master_Chain__c == 'SG') || (acc.DI_Master_Chain__c == 'WY'))))
If(acc.Property_Status__c=='LIVE' && acc.recordTypeId=='012200000004rPU')
mTotalGTAconnectIndependent +=1;
if(acc.ThresholdC__c=='Top Focus' && acc.ThresholdC__c!= null && acc.Grand_Total_RNs_2013__c != null){
GrandTotalRNs2013 = GrandTotalRNs2013 + acc.Grand_Total_RNs_2013__c;
}
}
}
system.debug('The Total number of accounts for this Master City is '+ mtotalLiveAccount);
system.debug('The no hotels in CityCenter : ' + mtotalCityCenterAccount);
system.debug('The no hotels in Airport : ' + mtotalAirportAccount);
system.debug('The no of hotels in POI is : ' + mtotalPOIAccount);
system.debug('The no of 3 star hotel here is : ' + mtotalthreestarHotels);
system.debug('The no of 4 star hotel here is : ' + mtotalfourstarHotels );
system.debug('The no of 5 star hotel here is : ' + mtotalfivestarHotels );
system.debug('The no of 2 star and less hotel is : ' + mtotalTwoStarorLess);
system.debug('The no of GTAconnectGlobal hotel is : ' + mTotalGTAconnectGlobal);
system.debug('The no of GTAconnectRegional hotel is : ' + mTotalGTAconnectRegional);
system.debug('The no of GTAconnectIndependent hotel is : ' + mTotalGTAconnectIndependent);
MCToUpdate.GTA_Hotels_2012__c = mtotalLiveAccount;
MCToUpdate.GTA_5_Hotels__c= mtotalfivestarHotels;
MCToUpdate.GTA_4_Hotels__c= mtotalfourstarHotels;
MCToUpdate.GTA_3_Hotels__c = mtotalthreestarHotels;
MCToUpdate.GTA_2_or_less_Hotels__c=mtotalTwoStarorLess;
MCToUpdate.GTA_City_Centre_Penetration__c=mtotalCityCenterAccount;
MCToUpdate.Suburbs_Airport__c= mtotalAirportAccount;
MCToUpdate.GTA_POI_Hotels__c= mtotalPOIAccount;
MCToUpdate.GTA_Global_Chain_Hotels__c=mTotalGTAconnectGlobal;
MCToUpdate.GTA_Regional_Chain_Hotels__c= mTotalGTAconnectRegional;
MCToUpdate.GTA_Independant__c = mTotalGTAconnectIndependent;
MCToUpdate.RN_Projection_2014_Top_Focus__c = GrandTotalRNs2013;
updateMasterCity.add(MCToUpdate);
}
//Finally update all affected cities:
update updateMasterCity;
}
Help for a new salesforce enthusiastic..Cance of improving this trigger for bulk loads? How can i use try catch?
Many Thanks!!
trigger updateGTAHotelCount on Account (after delete, after insert, after update) {
//The Triger will update the GTA Hotels Statistics for this year in Strategy Master City for the Accounts with Property Status = LIVE and RecordType= Property.
//The Account Object has lookup to City__c and city__c has a lookup to Pseudo_city__c object.
//This trigger will aggregate the total number of Accounts for a Master City, total number of Five star Account etc
public Set<id> cityIds = new Set<id>();
public Set<Id> mCityIds = new Set<Id>();
public Map<Id, Pseudo_City__c> MCitiesToUpdate = new Map<Id, Pseudo_City__c>();
public Map<Id,City__c> citiesToUpdate = new Map<Id,City__c>();
//Create a set of city Ids for the Accounts and Map of Cities to update
List<Pseudo_city__c> updateMasterCity = new List<Pseudo_city__c>();
if(trigger.isInsert || Trigger.isUpdate){
for(Account item : Trigger.new){
if (Item.recordTypeId=='012200000004rPU' && Item.Property_Status__c=='LIVE' )
cityIds.add(item.city_new__c);
}
system.debug('The list of cities for insert/Update-- ' + cityIds);
}
if(Trigger.isDelete){
for(Account item : Trigger.old){
if (Item.recordTypeId=='012200000004rPU' && Item.Property_Status__c=='LIVE' )
cityIds.add(item.city_new__c);
}
system.debug('The list of cities Delete -- ' + cityIds);
}
for(City__c Tempcity : [Select Master_City_Cluster__c from city__c where ID IN :cityIds ]){
mcityIds.add(Tempcity.Master_City_Cluster__c );
}
system.debug('The Master cities to be updated ' + mCityIds );
for(Pseudo_city__c mcity :[Select Id,GTA_3_Hotels__c,GTA_4_Hotels__c,GTA_5_Hotels__c,GTA_2_or_less_Hotels__c,GTA_Hotels_2012__c from Pseudo_City__c where Id in :mcityIds]){
MCitiesToUpdate.put(mcity.Id, mcity);
}
system.debug('The master City Details '+ MCitiesToUpdate );
for(City__c city :[Select Id, Name,Master_City_Cluster__c,City_Location__c,(Select Id,Name,Property_Status__c,Location__c,recordTypeId,Star_Rating__c,GTA_Connect_Chain__c,DI_Master_Chain__c,ThresholdC__c,Grand_Total_RNs_2013__c from city__c.Accounts1__r) FROM city__c where Master_City_Cluster__c IN :mcityIds]){
If(city.Master_City_Cluster__c != null )
citiesToUpdate.put(city.Id,city);
}
system.debug('The cities to be updated ' + citiesToUpdate);
//Loop through every mastercity, then cities for each master cities and then accounts for each city and count them total and then based on matching criteria
for(Pseudo_city__c MCToUpdate :MCitiesToUpdate.values()){
//Initialize the variable to 0
integer mtotalLiveAccount = 0;
integer mtotalfivestarHotels=0;
integer mtotalfourstarHotels =0;
integer mtotalthreestarHotels =0;
integer mTotalGTAconnectRegional = 0;
integer mTotalGTAconnectGlobal = 0;
integer mTotalGTAconnectIndependent = 0;
integer mtotalCityCenterAccount = 0;
integer mtotalAirportAccount=0;
integer mtotalRegionAccount =0;
integer mtotalPOIAccount=0;
integer mtotalTwoStarorLess=0;
double GrandTotalRNs2013=0;
//looping thru the Master cities
for(city__c citi : citiesToUpdate.values()){
//looping through the cities in this Master city
if(citi.City_Location__c == 'City Center' && citi.City_Location__c != null ) {
for(Account acc : citi.Accounts1__r){
If(acc.Property_Status__c=='LIVE' && acc.recordTypeId=='012200000004rPU')
mtotalCityCenterAccount+= 1;
}
system.debug('In city loop City Center Hotels : ' + mtotalCityCenterAccount);
}
if(citi.City_Location__c == 'Suburb & Airport' && citi.City_Location__c != null) {
for(Account acc : citi.Accounts1__r){
If(acc.Property_Status__c=='LIVE' && acc.recordTypeId=='012200000004rPU')
mtotalAirportAccount+= 1;
}
system.debug('In city loop Airport Hotels : ' + mtotalAirportAccount);
}
if(citi.City_Location__c == 'Point of interest' && citi.City_Location__c != null) {
for(Account acc : citi.Accounts1__r){
If(acc.Property_Status__c=='LIVE' && acc.recordTypeId=='012200000004rPU')
mtotalPOIAccount+= 1;
}
system.debug('In city loop POI Hotels : ' + mtotalPOIAccount);
}
for(Account acc : citi.Accounts1__r){
If(acc.Property_Status__c=='LIVE' && acc.recordTypeId=='012200000004rPU')
mtotalLiveAccount+= 1;
if (acc.Star_Rating__c=='5 Star' && acc.Star_Rating__c != null)
If(acc.Property_Status__c=='LIVE' && acc.recordTypeId=='012200000004rPU')
mtotalfivestarHotels += 1;
if (acc.Star_Rating__c=='4 Star' && acc.Star_Rating__c != null)
If(acc.Property_Status__c=='LIVE' && acc.recordTypeId=='012200000004rPU')
mtotalfourstarHotels += 1;
if (acc.Star_Rating__c=='3 Star' && acc.Star_Rating__c != null )
If(acc.Property_Status__c=='LIVE' && acc.recordTypeId=='012200000004rPU')
mtotalthreestarHotels += 1;
if(acc.Star_Rating__c =='1 Star' || acc.Star_Rating__c=='2 Star' || acc.Star_Rating__c== 'NA')
If(acc.Property_Status__c=='LIVE' && acc.recordTypeId=='012200000004rPU')
mtotalTwoStarorLess+=1;
// if (((acc.DI_Master_Chain__c != 'ACCOR') || (acc.DI_Master_Chain__c != 'BW') || (acc.DI_Master_Chain__c != 'DESIGN') || (acc.DI_Master_Chain__c != 'EC') || (acc.DI_Master_Chain__c != 'FAIRMONT') || (acc.DI_Master_Chain__c != 'HILTON') || (acc.DI_Master_Chain__c != 'HYATT') || (acc.DI_Master_Chain__c != 'IHG') || (acc.DI_Master_Chain__c != 'JUMEIRAH') || (acc.DI_Master_Chain__c != 'KI') || (acc.DI_Master_Chain__c != 'LUXE') || (acc.DI_Master_Chain__c != 'MC') || (acc.DI_Master_Chain__c != 'MK') || (acc.DI_Master_Chain__c != 'MOHG') || (acc.DI_Master_Chain__c != 'SG') || (acc.DI_Master_Chain__c != 'WY') ) && (acc.GTA_Connect_Chain__c != null) && ((acc.GTA_Connect_Chain__c != 'Intercontinental Hotels Group') || (acc.GTA_Connect_Chain__c != 'Accor Hotels') || (acc.GTA_Connect_Chain__c != 'Best Western International') || (acc.GTA_Connect_Chain__c != 'Carlson Hospitality') || (acc.GTA_Connect_Chain__c != 'Choice Hotels International') || (acc.GTA_Connect_Chain__c != 'Design Hotels') || (acc.GTA_Connect_Chain__c != 'Fairmont Raffles Swissotel Hotels & Resorts') || (acc.GTA_Connect_Chain__c != 'Four Seasons Hotels & Resorts') || (acc.GTA_Connect_Chain__c != 'Hilton Worldwide') || (acc.GTA_Connect_Chain__c != 'Hyatt Hotels and Resorts') || (acc.GTA_Connect_Chain__c != 'Jumeirah International LLC') || (acc.GTA_Connect_Chain__c != 'Kempinski') || (acc.GTA_Connect_Chain__c != 'KEMPINSKI HOTELS & RESORTS') || (acc.GTA_Connect_Chain__c != 'Langham Hotels') || (acc.GTA_Connect_Chain__c != 'Luxe Worldwide Hotels') || (acc.GTA_Connect_Chain__c != 'Mandarin Oriental Hotel Group') || (acc.GTA_Connect_Chain__c != 'Marriott International Inc.') || (acc.GTA_Connect_Chain__c != 'Millennium & Copthorne Hotels plc') || (acc.GTA_Connect_Chain__c != 'Moevenpick Hotels & Resorts') || (acc.GTA_Connect_Chain__c != 'Shangri-La Hotels and Resorts') || (acc.GTA_Connect_Chain__c != 'Starwood Hotels and Resorts') || (acc.GTA_Connect_Chain__c != 'The Ascott Group') || (acc.GTA_Connect_Chain__c != 'Wyndham Hotel Group' ) || (acc.GTA_Connect_Chain__c != 'independent' )))
// If(acc.Property_Status__c=='LIVE' && acc.recordTypeId=='012200000004rPU')
// mTotalGTAconnectRegional+= 1;
if (!((acc.DI_Master_Chain__c == 'ACCOR') || (acc.DI_Master_Chain__c == 'BW') || (acc.DI_Master_Chain__c == 'DESIGN') || (acc.DI_Master_Chain__c == 'EC') || (acc.DI_Master_Chain__c == 'FAIRMONT') || (acc.DI_Master_Chain__c == 'HILTON') || (acc.DI_Master_Chain__c == 'HYATT') || (acc.DI_Master_Chain__c == 'IHG') || (acc.DI_Master_Chain__c == 'JUMEIRAH') || (acc.DI_Master_Chain__c == 'KI') || (acc.DI_Master_Chain__c == 'LUXE') || (acc.DI_Master_Chain__c == 'MC') || (acc.DI_Master_Chain__c == 'MK') || (acc.DI_Master_Chain__c == 'MOHG') || (acc.DI_Master_Chain__c == 'SG') || (acc.DI_Master_Chain__c == 'WY') ) && (acc.GTA_Connect_Chain__c != null) && !((acc.GTA_Connect_Chain__c == 'Intercontinental Hotels Group') || (acc.GTA_Connect_Chain__c == 'Accor Hotels') || (acc.GTA_Connect_Chain__c == 'Best Western International') || (acc.GTA_Connect_Chain__c == 'Carlson Hospitality') || (acc.GTA_Connect_Chain__c == 'Choice Hotels International') || (acc.GTA_Connect_Chain__c == 'Design Hotels') || (acc.GTA_Connect_Chain__c == 'Fairmont Raffles Swissotel Hotels & Resorts') || (acc.GTA_Connect_Chain__c == 'Four Seasons Hotels & Resorts') || (acc.GTA_Connect_Chain__c == 'Hilton Worldwide') || (acc.GTA_Connect_Chain__c == 'Hyatt Hotels and Resorts') || (acc.GTA_Connect_Chain__c == 'Jumeirah International LLC') || (acc.GTA_Connect_Chain__c == 'Kempinski') || (acc.GTA_Connect_Chain__c == 'KEMPINSKI HOTELS & RESORTS') || (acc.GTA_Connect_Chain__c == 'Langham Hotels') || (acc.GTA_Connect_Chain__c == 'Luxe Worldwide Hotels') || (acc.GTA_Connect_Chain__c == 'Mandarin Oriental Hotel Group') || (acc.GTA_Connect_Chain__c == 'Marriott International Inc.') || (acc.GTA_Connect_Chain__c == 'Millennium & Copthorne Hotels plc') || (acc.GTA_Connect_Chain__c == 'Moevenpick Hotels & Resorts') || (acc.GTA_Connect_Chain__c == 'Shangri-La Hotels and Resorts') || (acc.GTA_Connect_Chain__c == 'Starwood Hotels and Resorts') || (acc.GTA_Connect_Chain__c == 'The Ascott Group') || (acc.GTA_Connect_Chain__c == 'Wyndham Hotel Group' ) || (acc.GTA_Connect_Chain__c == 'independent' )))
If(acc.Property_Status__c=='LIVE' && acc.recordTypeId=='012200000004rPU')
mTotalGTAconnectRegional+= 1;
if (((acc.GTA_Connect_Chain__c == 'Intercontinental Hotels Group') || (acc.GTA_Connect_Chain__c == 'Accor Hotels') || (acc.GTA_Connect_Chain__c == 'Best Western International') || (acc.GTA_Connect_Chain__c == 'Carlson Hospitality') || (acc.GTA_Connect_Chain__c == 'Choice Hotels International') || (acc.GTA_Connect_Chain__c == 'Fairmont Raffles Swissotel Hotels & Resorts') || (acc.GTA_Connect_Chain__c == 'Four Seasons Hotels & Resorts') || (acc.GTA_Connect_Chain__c == 'Hilton Worldwide') || (acc.GTA_Connect_Chain__c == 'Hyatt Hotels and Resorts') || (acc.GTA_Connect_Chain__c == 'Jumeirah International LLC') || (acc.GTA_Connect_Chain__c == 'Kempinski') || (acc.GTA_Connect_Chain__c == 'KEMPINSKI HOTELS & RESORTS') || (acc.GTA_Connect_Chain__c == 'Langham Hotels') || (acc.GTA_Connect_Chain__c == 'Luxe Worldwide Hotels') || (acc.GTA_Connect_Chain__c == 'Mandarin Oriental Hotel Group') || (acc.GTA_Connect_Chain__c == 'Marriott International Inc.') || (acc.GTA_Connect_Chain__c == 'Millennium & Copthorne Hotels plc') || (acc.GTA_Connect_Chain__c == 'Moevenpick Hotels & Resorts') || (acc.GTA_Connect_Chain__c == 'Shangri-La Hotels and Resorts') || (acc.GTA_Connect_Chain__c == 'Starwood Hotels and Resorts') || (acc.GTA_Connect_Chain__c == 'The Ascott Group') || (acc.GTA_Connect_Chain__c == 'Wyndham Hotel Group')) && !(acc.GTA_Connect_Chain__c == null))
If(acc.Property_Status__c=='LIVE' && acc.recordTypeId=='012200000004rPU')
mTotalGTAconnectGlobal+= 1;
if(((acc.GTA_Connect_Chain__c == 'independent') && !((acc.DI_Master_Chain__c == 'ACCOR') || (acc.DI_Master_Chain__c == 'BW') || (acc.DI_Master_Chain__c == 'EC') || (acc.DI_Master_Chain__c == 'FAIRMONT') || (acc.DI_Master_Chain__c == 'HILTON') || (acc.DI_Master_Chain__c == 'HYATT') || (acc.DI_Master_Chain__c == 'IHG') || (acc.DI_Master_Chain__c == 'JUMEIRAH') || (acc.DI_Master_Chain__c == 'KI') || (acc.DI_Master_Chain__c == 'MC') || (acc.DI_Master_Chain__c == 'MK') || (acc.DI_Master_Chain__c == 'MOHG') || (acc.DI_Master_Chain__c == 'SG') || (acc.DI_Master_Chain__c == 'WY'))))
If(acc.Property_Status__c=='LIVE' && acc.recordTypeId=='012200000004rPU')
mTotalGTAconnectIndependent +=1;
if(acc.ThresholdC__c=='Top Focus' && acc.ThresholdC__c!= null && acc.Grand_Total_RNs_2013__c != null){
GrandTotalRNs2013 = GrandTotalRNs2013 + acc.Grand_Total_RNs_2013__c;
}
}
}
system.debug('The Total number of accounts for this Master City is '+ mtotalLiveAccount);
system.debug('The no hotels in CityCenter : ' + mtotalCityCenterAccount);
system.debug('The no hotels in Airport : ' + mtotalAirportAccount);
system.debug('The no of hotels in POI is : ' + mtotalPOIAccount);
system.debug('The no of 3 star hotel here is : ' + mtotalthreestarHotels);
system.debug('The no of 4 star hotel here is : ' + mtotalfourstarHotels );
system.debug('The no of 5 star hotel here is : ' + mtotalfivestarHotels );
system.debug('The no of 2 star and less hotel is : ' + mtotalTwoStarorLess);
system.debug('The no of GTAconnectGlobal hotel is : ' + mTotalGTAconnectGlobal);
system.debug('The no of GTAconnectRegional hotel is : ' + mTotalGTAconnectRegional);
system.debug('The no of GTAconnectIndependent hotel is : ' + mTotalGTAconnectIndependent);
MCToUpdate.GTA_Hotels_2012__c = mtotalLiveAccount;
MCToUpdate.GTA_5_Hotels__c= mtotalfivestarHotels;
MCToUpdate.GTA_4_Hotels__c= mtotalfourstarHotels;
MCToUpdate.GTA_3_Hotels__c = mtotalthreestarHotels;
MCToUpdate.GTA_2_or_less_Hotels__c=mtotalTwoStarorLess;
MCToUpdate.GTA_City_Centre_Penetration__c=mtotalCityCenterAccount;
MCToUpdate.Suburbs_Airport__c= mtotalAirportAccount;
MCToUpdate.GTA_POI_Hotels__c= mtotalPOIAccount;
MCToUpdate.GTA_Global_Chain_Hotels__c=mTotalGTAconnectGlobal;
MCToUpdate.GTA_Regional_Chain_Hotels__c= mTotalGTAconnectRegional;
MCToUpdate.GTA_Independant__c = mTotalGTAconnectIndependent;
MCToUpdate.RN_Projection_2014_Top_Focus__c = GrandTotalRNs2013;
updateMasterCity.add(MCToUpdate);
}
//Finally update all affected cities:
update updateMasterCity;
}
for(Pseudo_city__c mcity :[Select Id,GTA_3_Hotels__c,GTA_4_Hotels__c,GTA_5_Hotels__c,GTA_2_or_less_Hotels__c,GTA_Hotels_2012__c from Pseudo_City__c where Id in :mcityIds]){
MCitiesToUpdate.put(mcity.Id, mcity);
}
Isn't as efficient as:
MCitiesToUpdate.putAll([Select Id,GTA_3_Hotels__c,GTA_4_Hotels__c,GTA_5_Hotels__c,GTA_2_or_less_Hotels__c,GTA_Hotels_2012__c from Pseudo_City__c where Id in :mcityIds]);
for(City__c city :[Select Id, Name,Master_City_Cluster__c,City_Location__c,(Select Id,Name,Property_Status__c,Location__c,recordTypeId,Star_Rating__c,GTA_Connect_Chain__c,DI_Master_Chain__c,ThresholdC__c,Grand_Total_RNs_2013__c from city__c.Accounts1__r) FROM city__c where Master_City_Cluster__c IN :mcityIds]){
If(city.Master_City_Cluster__c != null )
citiesToUpdate.put(city.Id,city);
}
Isn't efficient as:
citiesToUpdate.putAll([Select Id, Name,Master_City_Cluster__c,City_Location__c,(Select Id,Name,Property_Status__c,Location__c,recordTypeId,Star_Rating__c,GTA_Connect_Chain__c,DI_Master_Chain__c,ThresholdC__c,Grand_Total_RNs_2013__c from city__c.Accounts1__r) FROM city__c where Master_City_Cluster__c IN :mcityIds AND Master_City_Cluster__c != NULL]);
(moved the "if" criteria directly into the query for efficiency)
if(...) if(...)
Isn't as efficient as:
if(... && ...)
Example:
if (!((acc.DI_Master_Chain__c == 'ACCOR') || (acc.DI_Master_Chain__c == 'BW') || (acc.DI_Master_Chain__c == 'DESIGN') || (acc.DI_Master_Chain__c == 'EC') || (acc.DI_Master_Chain__c == 'FAIRMONT') || (acc.DI_Master_Chain__c == 'HILTON') || (acc.DI_Master_Chain__c == 'HYATT') || (acc.DI_Master_Chain__c == 'IHG') || (acc.DI_Master_Chain__c == 'JUMEIRAH') || (acc.DI_Master_Chain__c == 'KI') || (acc.DI_Master_Chain__c == 'LUXE') || (acc.DI_Master_Chain__c == 'MC') || (acc.DI_Master_Chain__c == 'MK') || (acc.DI_Master_Chain__c == 'MOHG') || (acc.DI_Master_Chain__c == 'SG') || (acc.DI_Master_Chain__c == 'WY') ) && (acc.GTA_Connect_Chain__c != null) && !((acc.GTA_Connect_Chain__c == 'Intercontinental Hotels Group') || (acc.GTA_Connect_Chain__c == 'Accor Hotels') || (acc.GTA_Connect_Chain__c == 'Best Western International') || (acc.GTA_Connect_Chain__c == 'Carlson Hospitality') || (acc.GTA_Connect_Chain__c == 'Choice Hotels International') || (acc.GTA_Connect_Chain__c == 'Design Hotels') || (acc.GTA_Connect_Chain__c == 'Fairmont Raffles Swissotel Hotels & Resorts') || (acc.GTA_Connect_Chain__c == 'Four Seasons Hotels & Resorts') || (acc.GTA_Connect_Chain__c == 'Hilton Worldwide') || (acc.GTA_Connect_Chain__c == 'Hyatt Hotels and Resorts') || (acc.GTA_Connect_Chain__c == 'Jumeirah International LLC') || (acc.GTA_Connect_Chain__c == 'Kempinski') || (acc.GTA_Connect_Chain__c == 'KEMPINSKI HOTELS & RESORTS') || (acc.GTA_Connect_Chain__c == 'Langham Hotels') || (acc.GTA_Connect_Chain__c == 'Luxe Worldwide Hotels') || (acc.GTA_Connect_Chain__c == 'Mandarin Oriental Hotel Group') || (acc.GTA_Connect_Chain__c == 'Marriott International Inc.') || (acc.GTA_Connect_Chain__c == 'Millennium & Copthorne Hotels plc') || (acc.GTA_Connect_Chain__c == 'Moevenpick Hotels & Resorts') || (acc.GTA_Connect_Chain__c == 'Shangri-La Hotels and Resorts') || (acc.GTA_Connect_Chain__c == 'Starwood Hotels and Resorts') || (acc.GTA_Connect_Chain__c == 'The Ascott Group') || (acc.GTA_Connect_Chain__c == 'Wyndham Hotel Group' ) || (acc.GTA_Connect_Chain__c == 'independent' )))
If(acc.Property_Status__c=='LIVE' && acc.recordTypeId=='012200000004rPU')
mTotalGTAconnectRegional+= 1;
Should be:
if (!((acc.DI_Master_Chain__c == 'ACCOR') || (acc.DI_Master_Chain__c == 'BW') || (acc.DI_Master_Chain__c == 'DESIGN') || (acc.DI_Master_Chain__c == 'EC') || (acc.DI_Master_Chain__c == 'FAIRMONT') || (acc.DI_Master_Chain__c == 'HILTON') || (acc.DI_Master_Chain__c == 'HYATT') || (acc.DI_Master_Chain__c == 'IHG') || (acc.DI_Master_Chain__c == 'JUMEIRAH') || (acc.DI_Master_Chain__c == 'KI') || (acc.DI_Master_Chain__c == 'LUXE') || (acc.DI_Master_Chain__c == 'MC') || (acc.DI_Master_Chain__c == 'MK') || (acc.DI_Master_Chain__c == 'MOHG') || (acc.DI_Master_Chain__c == 'SG') || (acc.DI_Master_Chain__c == 'WY') ) && (acc.GTA_Connect_Chain__c != null) && !((acc.GTA_Connect_Chain__c == 'Intercontinental Hotels Group') || (acc.GTA_Connect_Chain__c == 'Accor Hotels') || (acc.GTA_Connect_Chain__c == 'Best Western International') || (acc.GTA_Connect_Chain__c == 'Carlson Hospitality') || (acc.GTA_Connect_Chain__c == 'Choice Hotels International') || (acc.GTA_Connect_Chain__c == 'Design Hotels') || (acc.GTA_Connect_Chain__c == 'Fairmont Raffles Swissotel Hotels & Resorts') || (acc.GTA_Connect_Chain__c == 'Four Seasons Hotels & Resorts') || (acc.GTA_Connect_Chain__c == 'Hilton Worldwide') || (acc.GTA_Connect_Chain__c == 'Hyatt Hotels and Resorts') || (acc.GTA_Connect_Chain__c == 'Jumeirah International LLC') || (acc.GTA_Connect_Chain__c == 'Kempinski') || (acc.GTA_Connect_Chain__c == 'KEMPINSKI HOTELS & RESORTS') || (acc.GTA_Connect_Chain__c == 'Langham Hotels') || (acc.GTA_Connect_Chain__c == 'Luxe Worldwide Hotels') || (acc.GTA_Connect_Chain__c == 'Mandarin Oriental Hotel Group') || (acc.GTA_Connect_Chain__c == 'Marriott International Inc.') || (acc.GTA_Connect_Chain__c == 'Millennium & Copthorne Hotels plc') || (acc.GTA_Connect_Chain__c == 'Moevenpick Hotels & Resorts') || (acc.GTA_Connect_Chain__c == 'Shangri-La Hotels and Resorts') || (acc.GTA_Connect_Chain__c == 'Starwood Hotels and Resorts') || (acc.GTA_Connect_Chain__c == 'The Ascott Group') || (acc.GTA_Connect_Chain__c == 'Wyndham Hotel Group' ) || (acc.GTA_Connect_Chain__c == 'independent' )) && (acc.Property_Status__c=='LIVE' && acc.recordTypeId=='012200000004rPU'))
mTotalGTAconnectRegional+= 1;
I also feel that those IF statements could be cleaned up nicely by listing the values first, tthen the associated field to compare to using Sets. As the IF statements are right now, they are very difficult to read. The code should read more like this:
if( (new Set<String> { 'ACCOR', 'BW', 'DESIGN', 'EC', 'FAIRMONT', 'HILTON', 'HYATT', 'IHG', ... }).contains( acc.DI_Master_Chain ) &&
(new Set<String> { 'Intercontinental Hotels Group', 'Accor Hotels', 'Best Western International', 'Carlson Hospitality', ... }).contains(acc.GTA_Connect_Chain__c) ...
It greatly condenses the IF-THEN logic and makes it more legible. Also, don't be afraid to separate parts of the IF statements across multiple lines for legibility (nothing to do with efficiency, simply legibility).
Combine IF statements, when practical, by extracting their common denominator, like you would in algebra.
For example, 6x^2 + x - 35 could be written as (2x + 5)(3x - 7). and 5x+15 could be written as 5(x+3). Similarly:
if( A && X) { ... }
if( B && X) { ... }
if( C && X) { ... }
if( D && X) { ... }
Can be written as:
if(X) {
if(A) { ... }
if(B) { ... }
if(C) { ... }
if(D) { ... }
}
This means that instead of evaluating a ton of different if statements each loop, you can examine the common factor and exclude all the if statements *at once* if none of them would match.
I'm also fairly certain that your loop structure isn't nearly as neat as it should be, since you've a number of loops inside each other. Also, you could probably build an aggregate query to get most of the information you want without looping at all.
In other words, while this code works on small batches, I would not trust it to work on large sets of data, because of its naive approach. This code should work well enough for day-to-day operations, but I would expect it to have problems with larger batches (either slower or timing out completely).
All Answers
for(Pseudo_city__c mcity :[Select Id,GTA_3_Hotels__c,GTA_4_Hotels__c,GTA_5_Hotels__c,GTA_2_or_less_Hotels__c,GTA_Hotels_2012__c from Pseudo_City__c where Id in :mcityIds]){
MCitiesToUpdate.put(mcity.Id, mcity);
}
Isn't as efficient as:
MCitiesToUpdate.putAll([Select Id,GTA_3_Hotels__c,GTA_4_Hotels__c,GTA_5_Hotels__c,GTA_2_or_less_Hotels__c,GTA_Hotels_2012__c from Pseudo_City__c where Id in :mcityIds]);
for(City__c city :[Select Id, Name,Master_City_Cluster__c,City_Location__c,(Select Id,Name,Property_Status__c,Location__c,recordTypeId,Star_Rating__c,GTA_Connect_Chain__c,DI_Master_Chain__c,ThresholdC__c,Grand_Total_RNs_2013__c from city__c.Accounts1__r) FROM city__c where Master_City_Cluster__c IN :mcityIds]){
If(city.Master_City_Cluster__c != null )
citiesToUpdate.put(city.Id,city);
}
Isn't efficient as:
citiesToUpdate.putAll([Select Id, Name,Master_City_Cluster__c,City_Location__c,(Select Id,Name,Property_Status__c,Location__c,recordTypeId,Star_Rating__c,GTA_Connect_Chain__c,DI_Master_Chain__c,ThresholdC__c,Grand_Total_RNs_2013__c from city__c.Accounts1__r) FROM city__c where Master_City_Cluster__c IN :mcityIds AND Master_City_Cluster__c != NULL]);
(moved the "if" criteria directly into the query for efficiency)
if(...) if(...)
Isn't as efficient as:
if(... && ...)
Example:
if (!((acc.DI_Master_Chain__c == 'ACCOR') || (acc.DI_Master_Chain__c == 'BW') || (acc.DI_Master_Chain__c == 'DESIGN') || (acc.DI_Master_Chain__c == 'EC') || (acc.DI_Master_Chain__c == 'FAIRMONT') || (acc.DI_Master_Chain__c == 'HILTON') || (acc.DI_Master_Chain__c == 'HYATT') || (acc.DI_Master_Chain__c == 'IHG') || (acc.DI_Master_Chain__c == 'JUMEIRAH') || (acc.DI_Master_Chain__c == 'KI') || (acc.DI_Master_Chain__c == 'LUXE') || (acc.DI_Master_Chain__c == 'MC') || (acc.DI_Master_Chain__c == 'MK') || (acc.DI_Master_Chain__c == 'MOHG') || (acc.DI_Master_Chain__c == 'SG') || (acc.DI_Master_Chain__c == 'WY') ) && (acc.GTA_Connect_Chain__c != null) && !((acc.GTA_Connect_Chain__c == 'Intercontinental Hotels Group') || (acc.GTA_Connect_Chain__c == 'Accor Hotels') || (acc.GTA_Connect_Chain__c == 'Best Western International') || (acc.GTA_Connect_Chain__c == 'Carlson Hospitality') || (acc.GTA_Connect_Chain__c == 'Choice Hotels International') || (acc.GTA_Connect_Chain__c == 'Design Hotels') || (acc.GTA_Connect_Chain__c == 'Fairmont Raffles Swissotel Hotels & Resorts') || (acc.GTA_Connect_Chain__c == 'Four Seasons Hotels & Resorts') || (acc.GTA_Connect_Chain__c == 'Hilton Worldwide') || (acc.GTA_Connect_Chain__c == 'Hyatt Hotels and Resorts') || (acc.GTA_Connect_Chain__c == 'Jumeirah International LLC') || (acc.GTA_Connect_Chain__c == 'Kempinski') || (acc.GTA_Connect_Chain__c == 'KEMPINSKI HOTELS & RESORTS') || (acc.GTA_Connect_Chain__c == 'Langham Hotels') || (acc.GTA_Connect_Chain__c == 'Luxe Worldwide Hotels') || (acc.GTA_Connect_Chain__c == 'Mandarin Oriental Hotel Group') || (acc.GTA_Connect_Chain__c == 'Marriott International Inc.') || (acc.GTA_Connect_Chain__c == 'Millennium & Copthorne Hotels plc') || (acc.GTA_Connect_Chain__c == 'Moevenpick Hotels & Resorts') || (acc.GTA_Connect_Chain__c == 'Shangri-La Hotels and Resorts') || (acc.GTA_Connect_Chain__c == 'Starwood Hotels and Resorts') || (acc.GTA_Connect_Chain__c == 'The Ascott Group') || (acc.GTA_Connect_Chain__c == 'Wyndham Hotel Group' ) || (acc.GTA_Connect_Chain__c == 'independent' )))
If(acc.Property_Status__c=='LIVE' && acc.recordTypeId=='012200000004rPU')
mTotalGTAconnectRegional+= 1;
Should be:
if (!((acc.DI_Master_Chain__c == 'ACCOR') || (acc.DI_Master_Chain__c == 'BW') || (acc.DI_Master_Chain__c == 'DESIGN') || (acc.DI_Master_Chain__c == 'EC') || (acc.DI_Master_Chain__c == 'FAIRMONT') || (acc.DI_Master_Chain__c == 'HILTON') || (acc.DI_Master_Chain__c == 'HYATT') || (acc.DI_Master_Chain__c == 'IHG') || (acc.DI_Master_Chain__c == 'JUMEIRAH') || (acc.DI_Master_Chain__c == 'KI') || (acc.DI_Master_Chain__c == 'LUXE') || (acc.DI_Master_Chain__c == 'MC') || (acc.DI_Master_Chain__c == 'MK') || (acc.DI_Master_Chain__c == 'MOHG') || (acc.DI_Master_Chain__c == 'SG') || (acc.DI_Master_Chain__c == 'WY') ) && (acc.GTA_Connect_Chain__c != null) && !((acc.GTA_Connect_Chain__c == 'Intercontinental Hotels Group') || (acc.GTA_Connect_Chain__c == 'Accor Hotels') || (acc.GTA_Connect_Chain__c == 'Best Western International') || (acc.GTA_Connect_Chain__c == 'Carlson Hospitality') || (acc.GTA_Connect_Chain__c == 'Choice Hotels International') || (acc.GTA_Connect_Chain__c == 'Design Hotels') || (acc.GTA_Connect_Chain__c == 'Fairmont Raffles Swissotel Hotels & Resorts') || (acc.GTA_Connect_Chain__c == 'Four Seasons Hotels & Resorts') || (acc.GTA_Connect_Chain__c == 'Hilton Worldwide') || (acc.GTA_Connect_Chain__c == 'Hyatt Hotels and Resorts') || (acc.GTA_Connect_Chain__c == 'Jumeirah International LLC') || (acc.GTA_Connect_Chain__c == 'Kempinski') || (acc.GTA_Connect_Chain__c == 'KEMPINSKI HOTELS & RESORTS') || (acc.GTA_Connect_Chain__c == 'Langham Hotels') || (acc.GTA_Connect_Chain__c == 'Luxe Worldwide Hotels') || (acc.GTA_Connect_Chain__c == 'Mandarin Oriental Hotel Group') || (acc.GTA_Connect_Chain__c == 'Marriott International Inc.') || (acc.GTA_Connect_Chain__c == 'Millennium & Copthorne Hotels plc') || (acc.GTA_Connect_Chain__c == 'Moevenpick Hotels & Resorts') || (acc.GTA_Connect_Chain__c == 'Shangri-La Hotels and Resorts') || (acc.GTA_Connect_Chain__c == 'Starwood Hotels and Resorts') || (acc.GTA_Connect_Chain__c == 'The Ascott Group') || (acc.GTA_Connect_Chain__c == 'Wyndham Hotel Group' ) || (acc.GTA_Connect_Chain__c == 'independent' )) && (acc.Property_Status__c=='LIVE' && acc.recordTypeId=='012200000004rPU'))
mTotalGTAconnectRegional+= 1;
I also feel that those IF statements could be cleaned up nicely by listing the values first, tthen the associated field to compare to using Sets. As the IF statements are right now, they are very difficult to read. The code should read more like this:
if( (new Set<String> { 'ACCOR', 'BW', 'DESIGN', 'EC', 'FAIRMONT', 'HILTON', 'HYATT', 'IHG', ... }).contains( acc.DI_Master_Chain ) &&
(new Set<String> { 'Intercontinental Hotels Group', 'Accor Hotels', 'Best Western International', 'Carlson Hospitality', ... }).contains(acc.GTA_Connect_Chain__c) ...
It greatly condenses the IF-THEN logic and makes it more legible. Also, don't be afraid to separate parts of the IF statements across multiple lines for legibility (nothing to do with efficiency, simply legibility).
Combine IF statements, when practical, by extracting their common denominator, like you would in algebra.
For example, 6x^2 + x - 35 could be written as (2x + 5)(3x - 7). and 5x+15 could be written as 5(x+3). Similarly:
if( A && X) { ... }
if( B && X) { ... }
if( C && X) { ... }
if( D && X) { ... }
Can be written as:
if(X) {
if(A) { ... }
if(B) { ... }
if(C) { ... }
if(D) { ... }
}
This means that instead of evaluating a ton of different if statements each loop, you can examine the common factor and exclude all the if statements *at once* if none of them would match.
I'm also fairly certain that your loop structure isn't nearly as neat as it should be, since you've a number of loops inside each other. Also, you could probably build an aggregate query to get most of the information you want without looping at all.
In other words, while this code works on small batches, I would not trust it to work on large sets of data, because of its naive approach. This code should work well enough for day-to-day operations, but I would expect it to have problems with larger batches (either slower or timing out completely).
If some one can give me an outline on how can i use try catch in my trigger (actually i need to put this code in a method of a class which is called by trigger handler) as i am getting the error message when this trigger is executed independent of other triggers(as a method) called from the trigger handler below. I will include this trigger as a method once i test it standalone during batch run..
trigger AccountTrigger on Account ( //after delete,
after insert,
//after undelete,
//after update,
//before delete,
before insert,
before update) {
/*
* Get instance of the trigger handler for Account and pass on the call to the relevant method
*/
AccountTriggerHandler handler = new AccountTriggerHandler(Trigger.isExecuting, Trigger.size);
if(Trigger.isInsert && Trigger.isBefore){
handler.AccBeforeInsert(Trigger.new);
}
else if(Trigger.isInsert && Trigger.isAfter){
handler.AccAfterInsert(Trigger.new);
}
else if(Trigger.isUpdate && Trigger.isBefore){
handler.AccBeforeUpdate(Trigger.old, Trigger.new, Trigger.newMap);
}
} // end trigger
This is the error message i am getting when this trigger executes during the batch load..Pl suggest
Apex script unhandled trigger exception by user/organization: 00520000001N2X6/00Dg00000006sVh Source organization: 00D200000006pqz (null)
updateGTAHotelCount: System.LimitException: Apex CPU time limit exceeded
Many Thanks