I am trying to create a dynamic class to retrieve a record for update.
I struggle with the following part of the code:
targetSObject = new sor.getSObjectType()(ID = sObjectID);
For example, if this were an account record, I would want the result to be:
targetSobject = new Account(ID = '0016A000003tqQfQAI')
Essentially what I am asking is how do I recreate the above example using the Schema class.
Also, I am curious what the community thinks about retrieving records using the new Sobject(ID = '...')
public with sharing class UpdateAccountGoalsFromOpp {
Map<String, Map<Id, SObject>> sorToUpdate = new Map<String, Map<Id, SObject>>();
public SObject getSObject(ID sObjectID)
{
Schema.DescribeSObjectResult sor = sObjectID.getSobjectType().getDescribe();
String recObject = String.valueOf(sor.getName());
if(!sorToUpdate.containsKey(recObject))
{
sorToUpdate.put(recObject, new Map<Id, SObject>());
}
SObject targetSObject = sorToUpdate.get(recObject).get(sObjectID);
if(targetSObject == null)
{
targetSObject = new sor.getSObjectType()(ID = sObjectID);
sorToUpdate.get(recObject).put(sObjectID, targetSObject);
}
return targetSObject;
}
}
After doing research:
targetSObject = Schema.getGlobalDescribe().get(recObject).newSObject(sObjectID);
^ I think the above code may be the equivalent to:
targetSobject = new Account(ID = '0016A000003tqQfQAI')