You need to sign in to do that
Don't have an account?
DML not allowed on generic list error
I am getting a DML not allowed on generic list error when running some apex code that inserts a list of custom objects into the database. The code snippet below creates two custom objects and stores them as concrete types in a generic list (List<sObject>). However, when i try to insert the custom objects into the database using a single insert statement on the list, i get the DML not allowed on generic list error, even though when i inspect the output in the debug window I can clearly see that the list contains two concrete custom objects… Any idea why I am getting this error? This seems like an inconsistency...
code:
List<sObject> itemsToInsert = new List<sObject>();
sObject outputCategoryTypeSO = new AgreementLineItemCategoryType__c(AgreementLineItemCategory__c = outputCategorySO.Id);itemsToInsert.add(outputCategoryTypeSO);
outputCategoryTypeSO = new AgreementLineItemCategoryType__c(AgreementLineItemCategory__c = outputCategorySO.Id);itemsToInsert.add(outputCategoryTypeSO);
system.debug(LoggingLevel.INFO, 'itemsToInsert = '+itemsToInsert);
if (!itemsToInsert.isEmpty()) {
Database.insert(itemsToInsert);
}
debug log:
11:11:58:442 USER_DEBUG [210]|INFO|itemsToInsert = (AgreementLineItemCategoryType__c:{AdjustmentType__c=% Discount, AgreementLineItemCategory__c=a5GS00000004CxzMAE, PriceMethod__c=Per Unit, Product_Type__c=Maintenance, ItemSequence__c=1, PriceType__c=One Time, ConfigurationId__c=a3LS00000008btoMAA, IsOptionRollupLine__c=false, Quantity__c=1.00000, BasePriceMethod__c=Per Unit, Term__c=1, PriceUom__c=Each, SellingFrequency__c=One Time, LineNumber__c=1, ChargeType__c=Standard Price, Description__c=Acrobat Desk-09946063, AdjustmentAmount__c=2.00000, Frequency__c=One Time, LineType__c=Product/Service, SummaryGroupId__c=a3YS0000000CchoMAC, Uom__c=Each}, AgreementLineItemCategoryType__c:{AdjustmentType__c=% Discount, AgreementLineItemCategory__c=a5GS00000004CxzMAE, PriceMethod__c=Per Unit, Product_Type__c=Maintenance, ItemSequence__c=1, PriceType__c=One Time, ConfigurationId__c=a3LS00000008btoMAA, IsOptionRollupLine__c=false, Quantity__c=1.00000, BasePriceMethod__c=Per Unit, Term__c=1, PriceUom__c=Each, SellingFrequency__c=One Time, LineNumber__c=2, ChargeType__c=Standard Price, Description__c=Acrobat Desk-09946064, AdjustmentAmount__c=3.00000, Frequency__c=One Time, LineType__c=Product/Service, SummaryGroupId__c=a3YS0000000CchoMAC, Uom__c=Each})
11:11:58:442 DML_BEGIN [215]|Op:Insert|Type:SObject|Rows:2
11:11:58:443 EXCEPTION_THROWN [215]|System.TypeException: DML not allowed on generic List<SObject>
What is really interesting is that if i make a simply change to the code to insert the items one at a time it works:
if (!itemsToInsert.isEmpty()) {
for (sObject insert_item:itemsToInsert) {
system.debug(LoggingLevel.INFO, 'insert_item = '+insert_item);
Database.insert(insert_item);
}
}
Thanks
This is a known issue with apex. You can't perform dml operations on generic lists of sobjects. This is definitely a pita (especially for utility classes). I'd recommend casting your list to the appropriate type and then performing the dml operation.