+ Start a Discussion
The new LearnerThe new Learner 

How to cast sobject to a specific object in apex handler class

Hi Experts,

Can anyone let me know how to cast sobject to a specific object vice versa please. below i am getting the records, now i want to cast that object to specifi object.
SObjectType objectType = records.getSObjectType();
Footprints on the MoonFootprints on the Moon
Try with below code snippet-
Schema.SObjectType sObjectType = records.getSObjectType();
    if (sObjectType != null)
    {
        String listType = 'List<' + sObjectType + '>';
        List<SObject> castRecords = (List<SObject>)Type.forName(listType).newInstance();
        castRecords.addAll(records);
    }
Let me know if this helps!