[SalesForce] Dynamic Apex SObject Iteration

I've been trying for many hours to find the correct syntax in the documentation and via other searches in Google to get some dynamic code to run. Hopefully someone here will be able to provide the insight and proper syntax that I'm clearly not grasping. Here's some of my code that should illustrate what I'm trying to accomplish.

public list<sObject> generateGlobal_Id_Generator(list<sObject> TheRecords){

    if(!TheRecords.isEmpty()){ 
        system.assertEquals(TheRecords.getsObjectType,TheRecords[0].sObjectType());
        Schema.DescribesObjectResult DescribeResult = TheRecords[0].sObjectType.getDescribe();
        Schema.SObjectType objectType = DescribeResult.getSobjectType();
        getGlobal_Ids(TheRecords, objectType);

    }else{
        Throw CustomIdGenerationListIsEmptyException e;
    }
}

public list<sObject> getGlobal_Ids(list<sObject> TheRecords, Schema.SObjectType objectType){

    // I'm getting "invalid type sObjectType/invalid object error" depending on the coding
    // Below is what I want to do:

    for(SObject Record:TheRecords){
        Record.Global_Id__c = getGlobal_Id(ObjectName, Id.ValueOf(Record.Id));

       // the line above calls another function 'getGlobal_Id' & assigns results
    }   
    return TheRecords;
}

In the area above where I'm having problems, I've tried using sObject, string ObjectName = DescribeResults.getLocalName() and objectName = getPrefixedName(objectName);. (this is in a managed package). I've also tried using system.AssertEquals(ToGenerate.getType(),objectType); to set the equivalence between the list and object record.

There are 6 different objects that will use this function to dynamically generate this field value. What am I missing to make this work? Its as though I can't seem to 'cast' it properly to get it to allow me to iterate and also access field values as well.

Edit:

It seems that at the root of the problem I'm having is in casting the sObject to a string and back (esp when in a list). That seems to be what's preventing me from iterating on the object name (can't do it in string form nor as plain sObject). It seems I need to quickly determine the name of the sObject and maintain that relationship using some kind of particular syntax that I've seen before, but can't recall exactly what it is.

Best Answer

Here's my stab at what I think you're trying to do:

public class GlobalIds {

    public list<sObject> generateGlobal_Id_Generator(list<sObject> TheRecords){

        if(!TheRecords.isEmpty()){ 
            // I guess you mean to check that each element matches the sObjectType
            // reported by the list, so...
            for(sObject Record : TheRecords) {
                system.assertEquals(TheRecords.getSObjectType(),Record.getSObjectType());
            }
            return getGlobal_Ids(TheRecords, TheRecords[0].getSObjectType());

        }else{
            Throw new CustomIdGenerationListIsEmptyException();
        }
    }

    public list<sObject> getGlobal_Ids(list<sObject> TheRecords, Schema.SObjectType objectType){

        for(SObject Record:TheRecords){
            // Using .put() instead of assignment because it's a generic sObject
            // Using objectType.getDescribe().getName() because we've already checked that
            // the sObjectType is correct
            Record.put('Global_Id__c', getGlobal_Id(objectType.getDescribe().getName(), Id.ValueOf(Record.Id)));
        }   
        return TheRecords;
    }

    // Mocks so that I can compile this
    public String getGlobal_Id(String objName, Id objId) {
        return objName + objId;
    }
    public class CustomIdGenerationListIsEmptyException extends Exception {

    }    
}

I'm not sure that I can explain much more than just what's in the comments, but happy to answer questions on it. Or be told that I missed the point ;-)