function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ezdhanhussainezdhanhussain 

Add data to Generic List with multiple sobject List's

Hi I have some 9 sobjects data retrieved via SOQL. Now I want to add all this List's to a Generic List<sobejct> type to perform single DML operation

Can't this be done with out using For-loops ?
I tried addALL method od list
Best Answer chosen by ezdhanhussain
pconpcon
Yes, you can do this, you just have to typecast the list into a list of generic sObjects before adding them.
 
List<Account> accountList = new List<Account>();
List<Contact> contactList = new List<Contact>();

for (Integer i = 0; i < 10; i++) {
	accountList.add(new Account(Name = 'Boards Test Accout ' + i));
	contactList.add(new Contact(
            FirstName = 'Boards ' + i,
            LastName = 'Test Contact'
    ));
}

List<sObject> objects = new List<sObject>();
objects.addAll((List<sObject>)(accountList));
objects.addAll((List<sObject>)(contactList));
   
insert objects;

 

All Answers

pconpcon
Yes, you can do this, you just have to typecast the list into a list of generic sObjects before adding them.
 
List<Account> accountList = new List<Account>();
List<Contact> contactList = new List<Contact>();

for (Integer i = 0; i < 10; i++) {
	accountList.add(new Account(Name = 'Boards Test Accout ' + i));
	contactList.add(new Contact(
            FirstName = 'Boards ' + i,
            LastName = 'Test Contact'
    ));
}

List<sObject> objects = new List<sObject>();
objects.addAll((List<sObject>)(accountList));
objects.addAll((List<sObject>)(contactList));
   
insert objects;

 
This was selected as the best answer
ezdhanhussainezdhanhussain
Thank you pcon.. cheers :-)
sampath kumar 3sampath kumar 3
Hi pcon,

the above code is helpful for me but  can you tell me how to retrive data from this sobject like as below
for(sobject acc:objects)
{
    for(Account t:acc[0].account)
    {
        system.debug('....name...'+t.name);
    }       
}   

its not working can you suggest me how to fetch the data from the sobject by using forloop 
 
ezdhanhussainezdhanhussain
Hi Sampath,
Iterate through your generic sobject list and use instance of to find sobject type, and add it to your list of sobject.
list<object1> objectt1list = new list<object1>();
list<object2> objectt2list = new list<object2>();
for(SObject ss : genericlist){
	if(ss instanceof object1){
		objectt1list.add((object1)ss);
	}
	else if(ss instanceof object2){
		 objectt2list.add((object2) ss);
	}	
}


 
Don Schueler 18Don Schueler 18
I am using this basic approach and all is well..except that I want to sort on CreatedDate accross all the objectypes in my combined sObject List. Is there a simple way?
 
jitesh kumar 18jitesh kumar 18
heyyy pls solve it 

1) i have one text field name NEWONE on contact standard object in which i have to put  three id records of contacts
2) now i have to put that records in a list using slpit method and add in the list
    
how i can do that pls tell me ?
pconpcon
You can say
 
List<Id> ids = Contact.FieldName__c.split(';');

But be warned, if the field is empty this will throw a NPE so you'll want to make sure it's not null before you try to do that.
 
Ashu sharma 38Ashu sharma 38
Hi,

How to make an apex class as generic .

    @AuraEnabled
    public static list<Task> getContactTask(id recordId){
        system.debug('>>>>Record Id>>>>>'+recordId);
        list<Application__c> appList=[select id,Contact__c from Application__c where id=:recordId];
        list<Contact> cons=new list<Contact>();
        if (!appList.isEmpty()){
            list<Task> tsk=[select id,Subject,Priority,ActivityDate,Due_Date__c,
                            whoId from task where whoId=:appList[0].Contact__c];
            return tsk;
        }
        return null;
    },

As I tried using but how to pass task who id .