0

I am not able to create a Lookup to Task, EmailMessage and couple of other objects . So looks like there is a known limitation and hence Task and EmailMessage objects are not available in RelatedTo dropdown while creating lookup relationship field.

So I am trying to create objects for Task, EmailMessage etc dynamically and read records for these objects .

Can I place these object API names in meta data (Task, EmailMessage etc ) and read the string and split it based on delimeter and create objects ?

String ObjList = 'Task,EmailMessage' ( read from meta data)

sObject obj = Schema.getGlobalDescribe().get(lateBoundType).newSObject();

1 Answer 1

0

If you know the name of an sObject, it's as simple as:

sObject record = ((sObject)Type.forName('Schema',objectName).newInstance()).getSObjectType();

If you have multiple sObjects in a list, you can split that string and do that, too:

sObject[] records = new sObject[0];
for(String objectName: objList.split(',')) {
  records.add(((sObject)Type.forName('Schema',objectName).newInstance()).getSObjectType());
}

Although, more intuitively, I'd prefer to start with a list if I'm calling something objList:

String[] objList = new String[] { 'Tasking', 'EmailMessage' };

To get or set field values in an sObject, use the get(field) and put(field, value) methods, respectively.

You can read more on how to use these and other features in the Dynamic Apex documentation.

1
  • Thank you so much for your prompt response. You are really great
    – Hemant-2
    Oct 29, 2022 at 16:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .