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
lujandlujand 

DML not allowed on generic List<SObject>

Hi,

 

I am trying to write a clone method.  I got the error DML not allowed on generic List<SObject> on the line insert listOfNewSobjs; 

Any idea how I can resolve this?  Thanks in advance.

 

Here is the code:

 

List <sObject> listOfSobjs = new List<sObject> ();

List <sObject> listOfNewSobjs = new List<sObject>();

String queryString = 'Select ' + listOfFields + ' from ' + objTypeString + ' where ' + sCondition;

listOfSobjs= database.query(queryString);

for (sObject sobj : listOfSobjs)

{

   sObjNew = sobj.Clone(false, true);

   sObjNew.name = 'Clone : ' + sobj.name;

   listOfNewSobjs.add(sObjNew);

}

insert listOfNewSobjs;

VPrakashVPrakash

Hello, 

 

Try this 

 

List <sObject> listOfSobjs = new List<sObject> ();

List <sObject> listOfNewSobjs = new List<sObject>();

String queryString = 'Select ' + listOfFields + ' from ' + objTypeString + where ' + sCondition;

listOfSobjs= database.query(queryString);

for (sObject sobj : listOfSobjs)

{

   sObject sObjNew = sobj.Clone(false, true);

   sObjNew.name = 'Clone : ' + sobj.name;

   listOfNewSobjs.add(sObjNew);

}

insert listOfNewSobjs;

 

I think it would fix your issue 

lujandlujand

Thanks VPrakash.  I still got the error.

Damien_Damien_

I believe that you must cast from an sObject to whatever type you wish to insert.  sObject is a generic and can't directly be inserted into the database.

lujandlujand

Thanks Damien_. 

 

Found this http://boards.developerforce.com/t5/Apex-Code-Development/Dynamic-SObject-List-Creation-for-Object-that-Cannot-be-Queried/m-p/212615#M37765

 

So, after I changed my class API version from 19.0 to 20.0.  The error went away.