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.
recordId
andcustom object name
to another method that creates a 78 characterstring Id
and assigns it to a custom field calledGlobal_Id__c
that appears on 6 different custom objects. It will ultimately be called from a service on afterInsert.someSobj.getSobjectType()
returns the Type of the sobject; (2)someSobj.getSobjectType().getDescribe().getName()
returns the object's name; and (3)someSobj.get('ID')
gets the ID field's value without having to cast into a concrete type. What am I missing?