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
aKallNVaKallNV 

Need help with debugging 'An Internal Sever Error has occured'

Hi all,

The code below is a class with two methods. Both methods are called by an After Insert trigger on my custom obj called DeployedStrategy__c.

 

The createActionItems method is the one that I wrote first, and it does exactly what I want it to do. To briefly summarize what that is...I have four custom objs. Two of them define a template for Strategies and Action Items that belong to those strategies. The other two represent instances of those strategies...the main difference being that they are tied to an an Account record. So this method inserts all of the Action Items that belong to the Strategy as defined in the template. 

 

The checkForDupeStratys method is my attempt to handle a different business need, which is to prevent a Business Unit from creating more than one of the same Strategy. Business Units are assigned to instances of Strategies (DeployedStrategy__c) via a workflow rule, which simply updates the Unit__c field with the Unit defined on the creating user's record.  Therefore this method is attempting to pull all existing strategies that belong to the same Account as the newly inserted strategy and comapre if they use the same Strategy Template(Strategy__c field) and belong to the same Unit(Unit__c field). 

 

I get the following:  "An internal server error has occured"  and then it goes on to say that SFDC support has been notified, please provide any additional info I think would help, we applogize for the inconvenience, and an error ID.

 

I have never encountered one of these before and I'm not sure what's causing it. And I think it has to do the checkForDupeStratys method beause the other one is working fine when I use it on its own.

 

The thing that stumps me is that I have put system.assert(false,) throughout both methods to test for which lines of code are being hit....and on every line the system.assert message was thrown instead of the 'An Internal server error' message.

 

public with sharing class nvpsDeployedStrategyHandler {
	
	
	public static void checkForDupeStratys(List<DeployedStrategy__c> newStrats) {
		Set<id> acctIDs = new Set<id>();
		Map<ID, List<DeployedStrategy__c>> exStratys = new Map<ID, List<DeployedStrategy__c>>();//the IDs are Account IDs...a Map of Account IDs to the Strategies that belong to the Account
		
		//populates variables needed for remaining code.
        for(DeployedStrategy__c newStrat: newStrats) {
            acctIDs.add(newStrat.Account__c);
        }
        
        //The next two for loops were written to prevent the insertion of duplicate Strategies.
        for(DeployedStrategy__c exStrat : [select ID, Name, Account__c, Unit__c, Strategy__c from DeployedStrategy__c where Account__c IN :acctIDs AND Status__c = 'Active']) {
        	
        	List<DeployedStrategy__c> exSs = exStratys.get(exStrat.Account__c);
        	
        	if(null == exSs) {
        		exSs = new List<DeployedStrategy__c>();
        		exStratys.put(exStrat.Account__c, exSs);
        	}
        	exSs.add(exStrat);
        }
        
        for(DeployedStrategy__c ds : newStrats) {
        	for(DeployedStrategy__c eDS : exStratys.get(ds.Account__c)) {
        		if((ds.Unit__c == eDS.Unit__c) && (ds.Strategy__c == eDS.Strategy__c)) {
        			ds.addError(eds.Unit__c+' has already deployed this Strategy. Please refer to '+ds.Name);
        		}else {
        			createActionItems(newStrats);
        		}
        	}
        }         
	}

    public static void createActionItems(List<DeployedStrategy__c> newStrats) {
        
        Set<id> stratyTempIDs = new Set<id>(); //Set of Ids for all the Strategy Templates that the Inserted Strategies are related to.
        Map<Id,List<StrategyActionItem__c>> aiTemplates = new Map<Id,List<StrategyActionItem__c>>(); //Map of Action Item templates that belong to the same Strategy Template as the Inserted Strategies. Mapped to the Strategy Template Id.
        List<DeployedActionItems__c> newAIs = new List<DeployedActionItems__c>(); //A list of the new Action Items to be Inserted and related to the Inserted Strategies that came from the Trigger.
        
                
        //populates variables needed for remaining code.
        for(DeployedStrategy__c newStrat: newStrats) {
            stratyTempIDs.add(newStrat.Strategy__c);
        }     
        
        //The remaining code generates the Action Items that belong to each strategy.
        for(StrategyActionItem__c aiTemplate : [select ID, Name, Strategy__c, ItemNumber__c, ActionItem__r.Name from StrategyActionItem__c where Strategy__c IN :stratyTempIDs]) {
            // look for existing entry in map
            List<StrategyActionItem__c> aitemps = aiTemplates.get(aiTemplate.Strategy__c);
            
            if(null == aitemps) {                  
                aitemps = new List<StrategyActionItem__c>();
                aiTemplates.put(aiTemplate.Strategy__c, aitemps);
            }   
            aitemps.add(aiTemplate);
        }
        
        for(DeployedStrategy__c strat : newStrats) {
            for(StrategyActionItem__c aitemp : aiTemplates.get(strat.Strategy__c)) {
                DeployedActionItems__c newDAI = new DeployedActionItems__c( 
                    ActionItemName__c = aiTemp.Id,
                    Name__c = aiTemp.ActionItem__r.Name,
                    ItemNumber__c = aiTemp.ItemNumber__c,
                    Stage__c = 'Not Started',
                    KBaseMember__c = True,
                    DeployedStrategy__c = strat.Id
                );
            newAIs.add(newDAI);
            }           
        }   
        insert newAIs;  
    }
}