function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Guyver118Guyver118 

Will this trigger cause me problems when doing bulk operations?

Hi i am not sure if this trigger on the account will cause problems for bulk operations i just really want this to happen once for each current account on the interface when some user updates or inserts an account.

What the trigger does it checks a list of blocked account in another table and compares this with the current account address.

 

 

public class AddressManager_v1 { private static List<Address_Exclusion__c> exclusionList = new List<Address_Exclusion__c>(); private static Address_Manager__c exclusionManager; private static Map<String, Schema.SObjectField> conFields = Schema.sObjectType.Address_Exclusion__c.fields.getMap(); public static void SetAddressManager_v1() { exclusionManager = [ Select Id, Name, Description__c, Active__c, Valid_Objects__c From Address_Manager__c Where Name = 'Address Exclusion Manager' AND Active__c = TRUE Limit 1 ]; String sQuery = 'Select '; Integer i = 0; for(String f : conFields.keySet()) { i ++; if(i == conFields.keySet().size()) { sQuery += f; } else { sQuery += f + ', '; } } sQuery += ' FROM Address_Exclusion__c WHERE Address_Manager__c'; sQuery += ' = ' + '\'' + exclusionManager.Id + '\'' + ' And Active__c = TRUE'; exclusionList = Database.query(sQuery); } public static void CheckValidAccountAddress(List<Account> accList) { SetAddressManager_v1(); if(!exclusionList.isEmpty()) { for(Account a : accList) { List<String> billingStreet = a.BillingStreet.split('\n'); List<String> cleanedBillingStreet = new List<String>(); String billingCity = a.BillingCity.toLowerCase().trim(); String BillingPostalCode = a.BillingPostalCode.toLowerCase().trim(); String BillingCountry = a.BillingCountry.toLowerCase().trim(); for(String street : billingStreet) { cleanedBillingStreet.add(street.toLowerCase().trim()); } System.debug('### SPLIT STREET ADDRESS ###: ' + cleanedBillingStreet); System.debug('### EXCLUSION LIST ###: ' + exclusionList); for(Address_Exclusion__c exclusions : exclusionList) { List<String> streetLine = exclusions.Street__c.split('\n'); List<String> cleanedExclusionStreet = new List<String>(); String city = exclusions.City__c.toLowerCase().trim(); String postalCode = exclusions.Postal_Code__c.toLowerCase().trim(); String country = exclusions.Country__c.toLowerCase().trim(); for(String street : streetLine) { cleanedExclusionStreet.add(street.toLowerCase().trim()); } //In this case it goes through each street line and if any line matches //then this address is blocked. Does this need changing? //Also what other addresses can be checked on account i.e. shipping address etc.. for(String street1 : cleanedBillingStreet) { for(String street2 : cleanedExclusionStreet) { if(//Match street first split by new line (street1.equals(street2) //Match postal code next && billingPostalCode.equals(postalCode) //Match city next && billingCity.equals(city) //Finally match country && billingCountry.equals(country))) { a.addError('This Address has been blocked. Please contact the' + '\n system administrator'); } } } } } } } }

 

 

 

wesnoltewesnolte

Hey

 

Looks good. As long as you have no querys or DML inside a loop you should be good. The code you've pasted is a class and not a trigger though.. or am I missing something?

 

Cheers,

Wes 

Guyver118Guyver118
it is called in the trigger
wesnoltewesnolte

Ah.. still should be good. Just have to remember to keep any DB interaction outside of loops so you're all good.

 

Cheers,

Wes