+ Start a Discussion
k2018123k2018123 

System.TypeException: DML on generic List only allowed for insert, update or delete error

Hi I have  a trigger code in which when i am trying to upsert the list it is throwing me the error "System.TypeException: DML on generic List only allowed for insert, update or delete error". My unique external id field for the object is testfield__c. How do i handle the insert and update seperately?
Below is my code: 
Upsert operation highlighted in bold:
public class JobTriggerHandler
{
    private List<trigger_mapping__mdt> lstFieldMapping = null;
    private Schema.SObjectType sobType = null;
    public void onAfterInsert(List<Job_Control__c> lstNewObjA)
    {
        if (lstNewObjA.size() > 1 || (String.isBlank(lstNewObjA[0].Unique_Id__c ) || lstNewObjA[0].Status__c != 'Under Processing')){
            return;
        }
        
        Job_Control__c oJobControl = lstNewObjA[0];
        List<Job_Control__c> LstJobtoUpdateCRM = new List<Job_Control__c>();
        lstFieldMapping = [SELECT Source_Field_API_Name__c, Source_Object__c, Target_Field_API_Name__c, Target_Object__r.DeveloperName FROM trigger_mapping__mdt WHERE Target_Object__r.DeveloperName =:oJobControl.Object_Name__c ];
        sobType = Schema.getGlobalDescribe().get(lstFieldMapping[0].Target_Object__r.DeveloperName +'__c');
        processJobControlRecord(oJobControl.Unique_Id__c, oJobControl.Object_Name__c);
    }
    
    
    
    public void processJobControlRecord(String sUniqueId, String sObjectName)
    {
        if(lstFieldMapping ==null)
            lstFieldMapping = [SELECT Source_Field_API_Name__c, Source_Object__c, Target_Field_API_Name__c, Target_Object__r.DeveloperName FROM trigger_mapping__mdt WHERE Target_Object__r.DeveloperName =:sObjectName ];
        if(sobType == null)
            sobType = Schema.getGlobalDescribe().get(lstFieldMapping[0].Target_Object__r.DeveloperName +'__c');
        String sQueryForUniqueId = getQuery(sObjectName, sUniqueId);
        List<Staging_Course__c> lstStaging = Database.query(sQueryForUniqueId);
        List<sObject> lstSObject = new List<sObject>();
        
        
        for(Staging_Course__c oStag: lstStaging)
        {
            lstSObject.add(createNewRecordFromMapping(oStag, sObjectName));
            oStag.Status__c = 'Completed';
            oStag.Object_Name__c = sObjectName;
        }
        
        upsert lstSObject;
        update lstStaging;
        for(Job_Control__c  obj : [Select Id,Status__c from Job_Control__c where unique_id__c = :sUniqueId]){
            obj.Status__c = 'Completed'; 
            update obj ; 
            
        }
        
    }
    
    
    private String getQuery(String sObjectName, String sUniqueId)
    {
        
        String sQuery = 'SELECT Id ';
        for(trigger_mapping__mdt oMDT: lstFieldMapping)
        {
            sQuery += ',' + oMDT.Source_Field_API_Name__c;   
        }
        
        sQuery += ' FROM Staging_Course__c' + ' WHERE Unique_Id__c =: sUniqueId';
        return sQuery;
    }
    
    
    private sObject createNewRecordFromMapping(Staging_Course__c oStag, String sObjectName)
    {
        
        sObject sObjectNew = sobType.newSObject();
        for(trigger_mapping__mdt oMDT: lstFieldMapping)
        {
            sObjectNew.put(oMDT.Target_Field_API_Name__c, oStag.get(oMDT.Source_Field_API_Name__c));
        }
        
        return sObjectNew;
        
    }
}


 
Waqar Hussain SFWaqar Hussain SF
try belwo code
 
public class JobTriggerHandler
{
    private List<trigger_mapping__mdt> lstFieldMapping = null;
    private Schema.SObjectType sobType = null;
    public void onAfterInsert(List<Job_Control__c> lstNewObjA)
    {
        if (lstNewObjA.size() > 1 || (String.isBlank(lstNewObjA[0].Unique_Id__c ) || lstNewObjA[0].Status__c != 'Under Processing')){
            return;
        }
        
        Job_Control__c oJobControl = lstNewObjA[0];
        List<Job_Control__c> LstJobtoUpdateCRM = new List<Job_Control__c>();
        lstFieldMapping = [SELECT Source_Field_API_Name__c, Source_Object__c, Target_Field_API_Name__c, Target_Object__r.DeveloperName FROM trigger_mapping__mdt WHERE Target_Object__r.DeveloperName =:oJobControl.Object_Name__c ];
        sobType = Schema.getGlobalDescribe().get(lstFieldMapping[0].Target_Object__r.DeveloperName +'__c');
        processJobControlRecord(oJobControl.Unique_Id__c, oJobControl.Object_Name__c);
    }
    
    
    
    public void processJobControlRecord(String sUniqueId, String sObjectName)
    {
        if(lstFieldMapping ==null)
            lstFieldMapping = [SELECT Source_Field_API_Name__c, Source_Object__c, Target_Field_API_Name__c, Target_Object__r.DeveloperName FROM trigger_mapping__mdt WHERE Target_Object__r.DeveloperName =:sObjectName ];
        if(sobType == null)
            sobType = Schema.getGlobalDescribe().get(lstFieldMapping[0].Target_Object__r.DeveloperName +'__c');
        String sQueryForUniqueId = getQuery(sObjectName, sUniqueId);
        List<Staging_Course__c> lstStaging = Database.query(sQueryForUniqueId);
        List<Staging_Course__c> lstSObject = new List<Staging_Course__c>();
        
        
        for(Staging_Course__c oStag: lstStaging)
        {
            lstSObject.add(createNewRecordFromMapping(oStag, sObjectName));
            oStag.Status__c = 'Completed';
            oStag.Object_Name__c = sObjectName;
        }
        
        upsert lstSObject;
        update lstStaging;
        for(Job_Control__c  obj : [Select Id,Status__c from Job_Control__c where unique_id__c = :sUniqueId]){
            obj.Status__c = 'Completed'; 
            update obj ; 
            
        }
        
    }
    
    
    private String getQuery(String sObjectName, String sUniqueId)
    {
        
        String sQuery = 'SELECT Id ';
        for(trigger_mapping__mdt oMDT: lstFieldMapping)
        {
            sQuery += ',' + oMDT.Source_Field_API_Name__c;   
        }
        
        sQuery += ' FROM Staging_Course__c' + ' WHERE Unique_Id__c =: sUniqueId';
        return sQuery;
    }
    
    
    private sObject createNewRecordFromMapping(Staging_Course__c oStag, String sObjectName)
    {
        
        sObject sObjectNew = sobType.newSObject();
        for(trigger_mapping__mdt oMDT: lstFieldMapping)
        {
            sObjectNew.put(oMDT.Target_Field_API_Name__c, oStag.get(oMDT.Source_Field_API_Name__c));
        }
        
        return sObjectNew;
        
    }
}

 
k2018123k2018123
I dont see and difference in the code. Its the same .:(. Except the bold characters in <b> </b>
Waqar Hussain SFWaqar Hussain SF
I changed 
List<sObject> lstSObject = new List<sObject>();
To
List<Staging_Course__c> lstSObject = new List<Staging_Course__c>();

 
k2018123k2018123
Hi Waqar,

List<sObject> lstObject is fine coz ia ma getting sobject value from my custom metadata
Waqar Hussain SFWaqar Hussain SF
See the example of creating/upserting dynamic sObject in the comments of below idea. 

Which may help you
https://success.salesforce.com/ideaView?id=08730000000aNhVAAU