+ Start a Discussion
Jason Kuzmak 12Jason Kuzmak 12 

Trying to return a list of custom object records to my collection variable via apex action

Hello all,

I'm building my first flow, which is now in flow builder. My first major hurdle is that I can't seem to send a string to an apex action, query some records using that string as a soql criterion, then return the list. 

My Apex action looks like this:
 

global class Flow_ChecklistTemplateItemGetter{
	@invocableMethod(label = 'Get Template Items' description = 'gets all Checklist Template Items whose "Applies_To" field contains the provided machine model')
	global static List<Checklist_Template_Item__c> getRelevantTemplateItems(List<String> modelList){
		
		String searchModel;
		
		// this will only be a single value
		for(String s : modelList){
			searchModel = s;
		}
		
		System.debug('SearchModel = '+searchModel);

		String queryString = 'Select Id, Checkbox_Options__c, Checklist_Template_Section__c, Objective__c, Required__c, Sort_Order__c From Checklist_Template_Item__c Where Applies_To__c Includes(:searchModel)';
		List<Checklist_Template_Item__c> itemList = Database.query(queryString);
		
	 	for(Checklist_Template_Item__c cti : itemList){
	 		System.debug('checklist template item = '+cti);
	 	}
	 	
	 	return itemList;
	}
}
"modelList" is only ever going to be a single value, which is why I assign it to its own string for querying. 

I get the error "The number of results does not match the number of interviews that were executed in a single bulk execution request". 
Well...no kidding. I don't want to only return one record just because I only had one string input. 

What I thought might be the reason was that my output variable was not a collection variable, but the collection variable I created in followup to that is un-selectable from my Apex action. 

I can only select variables that accept a single record. 
User-added image

Best Answer chosen by Jason Kuzmak 12
Jason Kuzmak 12Jason Kuzmak 12
It turns out, this error may have been caused by the need to return a list of lists to export to a collection variable. I had thought that collection variables were lists of sObjects, but they're actually a list containing a single other list. I modified my code by inputting the list of lists of junction objects that connect my Checklist Template to my Checklist Template Items, then outputting a list of lists of template items that I had queried in my apex code.
global class Flow_ChecklistTemplateItemGetter{
	@invocableMethod(label = 'Get Template Items' description = 'gets all Checklist Template Items whose "Applies_To" field contains the provided machine model')
	global static List<List<Checklist_Template_Item__c>> getRelevantTemplateItems(List<List<Checklist_Template_Item_Junction__c>> junctionListList){
		
		System.debug('junctionListList.size() = '+junctionListList.size());
		Set<String> itemIds = new Set<String>();
		// this will only be a single value
		for(List<Checklist_Template_Item_Junction__c> junctionList : junctionListList){
			System.debug('junctionList = '+junctionList);
			for(Checklist_Template_Item_Junction__c ctij : junctionList){
				System.debug('ctij = '+ctij);
				itemIds.add(ctij.Checklist_Template_Item__c);
			}
		}
		
		List<Checklist_Template_Item__c> itemQuery = [Select Id, Checkbox_Options__c, Checklist_Template_Section__c, Objective__c, Required__c, Sort_Order__c From Checklist_Template_Item__c Where Id In : itemIds];
		System.debug('itemQuery.size() = '+itemQuery.size());
		
		List<List<Checklist_Template_Item__c>> itemListList = new List<List<Checklist_Template_Item__c>>();
		List<Checklist_Template_Item__c> itemList = new List<Checklist_Template_Item__c>();
		for(Checklist_Template_Item__c cti : itemQuery){
			System.debug('cti = '+cti);
			itemList.add(cti);
		}
		itemListList.add(itemList);
		System.debug('itemListList.size() = '+itemListList.size());
		
	 	return itemListList;
	}
}

Even more confusing, as above, only one SObject list is required in your list of lists, since the collection variable you pass to the invocable in the first place is a list containing a single list of records. When I tried to build multiple lists with individual records to store in the master list, I got "The number of results does not match the number of interviews that were executed in a single bulk execution request."