0

I'm extremely new to APEX, but it seemed like this was a simple enough requirement.

I have a custom field on the Account Record, and a list of 'bad codes' as a custom metadata setting. I need the APEX to check if the custom field on the Account starts with any of the Name__c values from the Custom Metadata List

Here's my code so far. It saved, but it doesn't update anything on the Account Record when I make an edit.

Any help, resources, or direction is greatly appreciated.

public class NaicsExclusion {

public static void CompareCodes(Account[] acc){
for (Account a :acc){
    List<NAICS_Exclusion__mdt> BadCodes =
        [SELECT Name__c FROM NAICS_Exclusion__mdt];
        for(NAICS_Exclusion__mdt BC : BadCodes) {
            System.debug('Name__c: ' + BC.Name__c);
            if (a.NaicsCode.StartsWith(BC.Name__c)) {
                a.isNAICSBad__c = true;
                Update a;
            }
            else {
                a.isNAICSBad__c = false;
                update a;

            }
        }
    }
  }
}

Here's the trigger code:

trigger AccountTrigger on Account (before insert, after insert, before update, 
after update)  
{
Trigger_Controls__c triggerSetting = Trigger_Controls__c.getOrgDefaults();

//if true, execute body of code
if(triggerSetting.AccountTrigger__c) {

    //Create an instance of handler class    
    AccountTriggerHandler handler = new AccountTriggerHandler();

    if(trigger.isBefore && trigger.isInsert){
        system.debug('=== On Before Insert Trigger');

        handler.AccountTriggerRoundRobin(trigger.new, trigger.newMap, 
trigger.oldMap);
        NaicsExclusion.CompareCodes(Trigger.new);
    }

    if(trigger.isAfter && trigger.isInsert){
        system.debug('=== On After Insert Trigger');    

        handler.updateContactOwner(Trigger.New, null);
        handler.agreementModification(Trigger.New);
        NaicsExclusion.CompareCodes(Trigger.new);

        //Put account Ids into List or Set so that @future method will accept them.
        Map<Id,Account> acctMap = Trigger.NewMap;
        Set<Id> accIds = acctMap.keySet();        
        //Call method which creates Box folder hierarchy
        createBoxFolder.createFolderHierarchy(accIds);
        NaicsExclusion.CompareCodes(Trigger.new);
    }

    if(trigger.isBefore && trigger.isUpdate){
        system.debug('=== On Before Update Trigger');

       /* handler.AccountTriggerRoundRobin(trigger.new, trigger.newMap, 
trigger.oldMap); */
    }

    if(trigger.isAfter && trigger.isUpdate){
        system.debug('=== On After Update Trigger');

        if( AccountTriggerHandler.runonce() < 5 ){
            handler.agreementModification(Trigger.New);
        }

        //Call the recurssion mechanism - which prevents the functionalities of the 
trigger to be invoked again and again        
        if(globalClass.avoidRecursion('AccountTrigger')){
           //return;
        }            
        handler.updateContactOwner(Trigger.New, Trigger.OldMap);

    }

    if(trigger.isBefore && trigger.isDelete){
        system.debug('=== OnBeforeDelete Trigger');

        //blank
    }

    if(trigger.isAfter && trigger.isDelete){
        system.debug('=== On After Delete Trigger');

        //blank
    }
}
}
  • 1
    Are you calling this from a trigger? Can you include the trigger code? – David Reed Nov 15 '19 at 18:12
  • Trigger Code added in the initial post – Nick Hodge Nov 15 '19 at 19:39
2

I cleaned up your code a little bit to fix some Bulkifying pitfalls, but to David Reed's point we still need your Trigger code to help you out further:

public class NaicsExclusion
{
    public static void CompareCodes(Account[] accs)
    {
        List<NAICS_Exclusion__mdt> BadCodes = [SELECT Name__c FROM NAICS_Exclusion__mdt];

        for(Account a :accs)
        {
            for(NAICS_Exclusion__mdt bc : BadCodes)
            {
                if(a.NaicsCode.StartsWith(bc.Name__c))
                {
                    a.isNAICSBad__c = true;
                    break;
                }else
                {
                    a.isNAICSBad__c = false;
                }
            }
        }

        update accs;
    }
}
  • 2
    you really should pivot the MDT into a map keyed by Name__c so the inner loop is more efficient – cropredy Nov 15 '19 at 19:28
  • Trying to post the trigger code, but it's a mess too (and too large to post here). I've spent the better part of a year cleaning up as much as I can (I'm the 4th admin in two years) – Nick Hodge Nov 15 '19 at 19:30
  • 1
    @cropredy I actually wanted to change it to a set of Strings, but I wanted to keep it as close to the original code for OP's sake – Bryan Anderson Nov 15 '19 at 19:30
  • I will welcome literally anything to make this work better. All of you are fantastic! – Nick Hodge Nov 15 '19 at 19:33
  • I'm getting the following error - Variable does not exist: bcSet at line 11 column 20 – Nick Hodge Nov 15 '19 at 19:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.