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 

SOQL query only finding one record when it should be finding several

 

Select ID, Name, Strategy__c, ActionItem__c, ActionItem__r.Name, ItemNumber__c from nvpsStrategyActionItem__c where Strategy__c IN :stIDs

 

 

I am using this query in a soql for loop to create a map. Through testing in the sandbox interface, I am expecting this to return 6 records but it only finds one, which is one of the records it is supposed to find.

Strategy__c is a Lookup field, and stIDs is a list of ids for the object it is looking up to. The query is basically trying to pull all of the child records of a parent record. Can anybody spot why this isn't working correctly?

 

thanks!

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

In this section of code:

 

 

for(nvpsStrategyActionItem__c aiTemplate : [select ID, Name, Strategy__c, ItemNumber__c, ActionItem__r.Name from nvpsStrategyActionItem__c where Strategy__c IN :stratyTempIDs]) {
	List<nvpsStrategyActionItem__c> aitemps = new List<nvpsStrategyActionItem__c>();
	aitemps.add(aiTemplate);
	aiTemplates.put(aiTemplate.Strategy__c, aitemps);
}

 

You are still overwriting the value for the key, just with a single element list rather then the single element.  You'll need to check if there is an existing value in the map for the key and append to that.

 

Here's some revised code that should do the trick:

 

 

for(nvpsStrategyActionItem__c aiTemplate : [select ID, Name, Strategy__c, ItemNumber__c, ActionItem__r.Name from nvpsStrategyActionItem__c where Strategy__c IN :stratyTempIDs]) {
        // look for existing entry in map
	List<nvpsStrategyActionItem__c> aitemps = aiTemplates.get(aiTemplate.Strategy__c);

        if (null==aitemps)
        {
           // no existing entry - create one and add to map
           aitemps=new List<nvpsStrategyActionItem__c>();
    	   aiTemplates.put(aiTemplate.Strategy__c, aitemps);
        }
     
        // add the value to the list
	aitemps.add(aiTemplate);
}

 

 

 

 

All Answers

bob_buzzardbob_buzzard

The query itself looks fine.  Can you post any more of the code?  Its a little difficult to suggest anything without knowing how you have generated the stIDs list.

 

 

 

 

bob_buzzardbob_buzzard

One more thing - as you are generating a map, what are you using the key the entries in the map?  If the key is the same for each returned record, they will just overwrite each other.

aKallNVaKallNV

I think your last post is the solution, but I'm not quite sure yet what to do about it. I'm going to think about it a little and post back here if I struggle.

 

Thanks!

bob_buzzardbob_buzzard

Cool.  One tip in this scenario is to make the values in your map a list rather than a single instance, then you can have multiple values associated with a single key.

aKallNVaKallNV

Ok. So I got a chance to work on this tonight, and thought I had worked out based on your advice, but it does the same **bleep** thing. So here is all of the code.

 

Trigger:

 

trigger nvpsStrategyAfterInsert on nvpsDeployedStrategies__c (after insert) {
	
	List<nvpsDeployedStrategies__c> newStratys = new List<nvpsDeployedStrategies__c>();//List of incoming Strategies that have been related to a Strategy Template
	
	for(nvpsDeployedStrategies__c newStrat : Trigger.new) {
		if(Null != newStrat.Strategy__c) {
			newStratys.add(newStrat);
		}		
	}
	nvpsDeployedStrategyHandler.createActionItems(newStratys);	
}

 

 

Class:

 

 

public with sharing class nvpsDeployedStrategyHandler {

	public static void createActionItems(List<nvpsDeployedStrategies__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<nvpsStrategyActionItem__c>> aiTemplates = new Map<Id,List<nvpsStrategyActionItem__c>>(); //Map of Action Item templates that belong to the same Strategy Template as the Inserted Strategies. Mapped to the Strategy Template Id.
		List<nvpsDeployedActionItems__c> newAIs = new List<nvpsDeployedActionItems__c>(); //A list of the new Action Items to be Inserted and related to the Inserted Strategies that came from the Trigger.
		
		for(nvpsDeployedStrategies__c newStrat: newStrats) {
			stratyTempIDs.add(newStrat.Strategy__c);
		}
		
		for(nvpsStrategyActionItem__c aiTemplate : [select ID, Name, Strategy__c, ItemNumber__c, ActionItem__r.Name from nvpsStrategyActionItem__c where Strategy__c IN :stratyTempIDs]) {
			List<nvpsStrategyActionItem__c> aitemps = new List<nvpsStrategyActionItem__c>();
			aitemps.add(aiTemplate);
			aiTemplates.put(aiTemplate.Strategy__c, aitemps);
		}
		
		for(nvpsDeployedStrategies__c strat : newStrats) {
			for(nvpsStrategyActionItem__c aitemp : aiTemplates.get(strat.Strategy__c)) {
				nvpsDeployedActionItems__c newDAI = new nvpsDeployedActionItems__c( 
					Name__c = aiTemp.ActionItem__r.Name,
					ItemNumber__c = aiTemp.ItemNumber__c,
					Stage__c = 'Not Started',
					StartDate__c = date.Today(),
					DeployedStrategy__c = strat.Id
				);
			newAIs.add(newDAI);
			}			
		}	
		insert newAIs;	
	}	
}

 

 

 

 

bob_buzzardbob_buzzard

In this section of code:

 

 

for(nvpsStrategyActionItem__c aiTemplate : [select ID, Name, Strategy__c, ItemNumber__c, ActionItem__r.Name from nvpsStrategyActionItem__c where Strategy__c IN :stratyTempIDs]) {
	List<nvpsStrategyActionItem__c> aitemps = new List<nvpsStrategyActionItem__c>();
	aitemps.add(aiTemplate);
	aiTemplates.put(aiTemplate.Strategy__c, aitemps);
}

 

You are still overwriting the value for the key, just with a single element list rather then the single element.  You'll need to check if there is an existing value in the map for the key and append to that.

 

Here's some revised code that should do the trick:

 

 

for(nvpsStrategyActionItem__c aiTemplate : [select ID, Name, Strategy__c, ItemNumber__c, ActionItem__r.Name from nvpsStrategyActionItem__c where Strategy__c IN :stratyTempIDs]) {
        // look for existing entry in map
	List<nvpsStrategyActionItem__c> aitemps = aiTemplates.get(aiTemplate.Strategy__c);

        if (null==aitemps)
        {
           // no existing entry - create one and add to map
           aitemps=new List<nvpsStrategyActionItem__c>();
    	   aiTemplates.put(aiTemplate.Strategy__c, aitemps);
        }
     
        // add the value to the list
	aitemps.add(aiTemplate);
}

 

 

 

 

This was selected as the best answer
aKallNVaKallNV

Yes! Thank you Bob.