I'm trying to emphasis separation of concerns with new code that I am writing. For an example that I am working on I want to create a custom object record when a different object record is edited to meet the criteria. Thus, I wrote my methods like this:
public override void afterInsert(Map<Id, SObject> newMapGeneric){
Map<Id, Competitive_Intel__c> newMap = (Map<Id, Competitive_Intel__c>) newMapGeneric;
if(!isRecursive)
{
isRecursive = true;
Set<Id> filtered = createCompanyNameFilter(newMap);
createCompanyName(filtered);
}
}
public static Set<Id> createCompanyNameFilter (Map<Id, Competitive_Intel__c> newMap)
{
Set<Id> cis = new Set<Id>();
Map<ID,Schema.RecordTypeInfo> rt_Map = Competitive_Intel__c.sObjectType.getDescribe().getRecordTypeInfosById();
for (Competitive_Intel__c ci : newMap.values())
{
if(...)
{
cis.add(ci.Id);
}
}
return cis;
}
public static List<Competitive_Intelligence_Company_Name__c> CreateCompanyName (Set<Id> cis)
{
List<Competitive_Intelligence_Company_Name__c> cisCoNameToInsert = new List<Competitive_Intelligence_Company_Name__c>();
List<Competitive_Intel__c> ci = [SELECT Id,
Company_Name__c
FROM Competitive_Intel__c
WHERE Id IN :cis];
for(Competitive_Intel__c cisLoop : ci)
{
cisCoNameToInsert.add(new Competitive_Intelligence_Company_Name__c(
Name = cisLoop.Company_Name__c,
Competitive_Intelligence_Id__c = cisLoop.Id
));
}
return cisCoNameToInsert;
}
public static void safeCreateCompanyName (List<Competitive_Intelligence_Company_Name__c> cisCoNameToInsert)
{
NPD_CompetitiveIntelligenceActions.createCompanyName(cisCoNameToInsert);
}
I'm stuck in that I can't figure out how to pass the list from createCompanyName to safeCreateCompanyName. When I write:
return cisCoNameToInesert;
safeCreateCompanyName(cisCoNameToInsert);
to pass it along to the next method I get
Non-void method might not return a value or might have statement after a return statement.
If I write:
if(!isRecursive)
{
isRecursive = true;
Set<Id> filtered = createCompanyNameFilter(newMap);
createCompanyName(filtered);
safeCreateCompanyName(cisCoNameToInsert);
}
To pass it along in the override method I get variable does not exist. How can I move lists from one method to another effectively?
cisCoNameToInsert
is a local variable meaning its name can't be referenced outside the method it is defined in. But the data can be referenced as the return value of the method as Adrian has explained in his answer.