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

Bulkify Trigger Challenges
trigger NAICSUpdate on Account (before insert, before update) { List<NAICS_Code__c> codes = [SELECT Name, Description__c from NAICS_CODE__C]; Set<NAICS_CODE__C> setcodes = new set <NaICS_code__c>(); setcodes.addAll(codes); for (Account acct :Trigger.new) { if(acct.NaicsCode != NULL) { try { string twoDigit = acct.NaicsCode.left(2); string threeDigit = acct.NaicsCode.left(3); NAICS_Code__c nCode2desc = [SELECT Name, Description__c from NAICS_CODE__C where Name = :twoDigit limit 1]; NAICS_Code__c nCode3desc = [SELECT Name, Description__c from NAICS_CODE__C where Name = :threeDigit limit 1]; acct.NAICS_2_Digit_Desc__c = nCode2desc.Description__c; acct.NAICS_3_Digit_Desc__c = nCode3desc.Description__c; } catch (Exception e) { system.debug('Failed'); } } } }
I'm trying to remove the two queries in the for loop. I can't use a map since I'm searching by name and not ID. I'm not really sure how to utilize set to return the right description if I use contains. Any help would greatly be appreciated.
All Answers