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?