0

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?

1
  • 2
    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.
    – Keith C
    Commented Apr 19, 2017 at 16:44

2 Answers 2

5

You just use the returned result:

safeCreateCompanyName(createCompanyName(filtered));

Or you can cache the result if you find it less confusing:

List<MyObject> companyNames = createCompanyname(filtered);
safeCreateCompanyName(companyNames);

Note that your naming is itself quite confusing. If you return a List, the name should be plural. If you return a single instance, then it should be singular.


In context, that would look like:

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);
        safeCreateCompanyName(createCompanyName(filtered));
    }
}

Note that you could further condense this logic, as you don't need to cache filtered, either:

if(!isRecursive)
{
    isRecursive = true; 
    safeCreateCompanyName(createCompanyName(createCompanyNameFilter(newMap)));
}
2

You need to pass like this:

if(!isRecursive)
        {
            isRecursive = true;
            Set<Id> filtered = createCompanyNameFilter(newMap);
            List<Competitive_Intelligence_Company_Name__c> lstCompInt = createCompanyName(filtered);
            safeCreateCompanyName(lstCompInt);
        }

One suggestion, in this createCompanyNameFilter() method, you are making a describe call. It will be better to put in cache, preferably Platform cache, to hold recordtypeId and names.

Map<ID,Schema.RecordTypeInfo> rt_Map = Competitive_Intel__c.sObjectType.getDescribe().getRecordTypeInfosById();
3
  • You don't need to. That is one way but not the only one. :)
    – Adrian Larson
    Commented Apr 19, 2017 at 16:52
  • My thought process was one method to do the filter returning the set of Ids, then create a list to of the different object then pass to this other class that is public without sharing so it can just run the action uninterrupted. Commented Apr 19, 2017 at 16:53
  • @DanWooding, I have updated my ans Commented Apr 19, 2017 at 16:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .