You need to sign in to do that
Don't have an account?
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;
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
Thanks VPrakash. I still got the error.
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.
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.