3

We currently have 2 different classes of code doing essentially the same thing on both the lead and account objects- grab the country and postal code and assign a territory. We are looking to combine the code into 1 class that accepts a list of sObjects, casts them appropriately, and then fills out the appropriate fields on the accounts or leads we pass through. Essentially something like the below is what we are looking for:

public class territoryUpdater {

    public static void territoryUpdater(string objectType, list<sObject> records) {

        string listType = 'list<'+objectType+'>';


        for ((sObject)Type.forName(objectType) r : records) {

            string postalCode;
            string country;

            if (objectType == 'Account') {
                postalCode = r.BillingPostalCode;
                country = r.BillingCountry;
            } 

            if (objectType == 'Lead') {
                postalCode = r.PostalCode;
                country = r.Country;

            }

            //do logic here...

            if (objectType == 'Account') {
                r.BillingPostalCode = postalCode;
                r.BillingCountry = country;
                r.Territory__c = whateverTerritoryIsFound;
            } 
            if (objectType == 'Lead') {
                r.PostalCode = postalCode;
                r.Country = country;
                r.Territory__c = whateverTerritoryIsFound;
            }
        }

        update records;     
    }
}

Obviously this code does not work, however I think it gets the point across of what I'm going for here. Does anyone have any input as to how something like this can be built?

2 Answers 2

3

You can do this:

public static void updateTerritory(SObject[] records, SObjectField countryField, SObjectField postalCodeField, SObjectField territoryField) {
    for(SObject record: records) {
        String postalCode = (String)record.get(postalCodeField);
        String country = (String)record.get(countryField);
        // Do logic
        record.put(countryField, country);
        record.put(postalCodeeField, postalCode);
        record.put(territoryField, whateverTerritoryIsFound);
    }
    update records;
}

It's called like this:

updateTerritory(listOfLeads, Lead.Country, Lead.PostalCode, Lead.Territory__c);
0
1

Since there appears to be zero shared logic between the two objects, I recommend simply overloading the method with object specific signatures.

public static void doStuff(List<Account> records)
{
    for (Account record : records)
    {
        // logic here!
    }
    update records;
}
public static void doStuff(List<Lead> records)
{
    for (Lead record : records)
    {
        // logic here!
    }
    update records;
}
1
  • The logic is exactly the same with the exception of the field names (postal code on lead is Lead.PostalCode whereas on account it's Account.BillingPostalCode). What you have there is what we are currently doing, but we figure if they are doing the same thing it would make sense to combine them Commented Jan 12, 2017 at 15:13

You must log in to answer this question.

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