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
k2018123k2018123 

invalid conversion from runtime type List to List in trigger

Hi,
I have. a trigger where is want to udate the record of list "lstNewObjA" . But using the code snippet below, it throws me the error "invalid conversion from runtime type List to List ". I want to update the list after DML operations on lstSObject and lstStaging are performed. Can anybody please help? Below is the code snippet:
public class JobTriggerHandler
{
    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>();
        processJobControlRecord(oJobControl.Unique_Id__c, oJobControl.Object_Name__c);
        
    }
    
    
    
    public void processJobControlRecord(String sUniqueId, String sObjectName)
    {
        String sQueryForUniqueId = getQuery(sObjectName, sUniqueId);
        List<Staging_Course__c> lstStaging = Database.query(sQueryForUniqueId);
        List<sObject> lstSObject = new List<sObject>();
        List<Job_Control__c> lstJobControl = Database.query(sQueryForUniqueId);
        for(Staging_Course__c oStag: lstStaging)
        {
            lstSObject.add(createNewRecordFromMapping(oStag, sObjectName));
            oStag.Status__c = 'Completed';
        }
        
        insert lstSObject;
        update lstStaging;
       /* for(Job_Control__c objAUpdated : lstJobControl)
            {
            Job_Control__c j = new Job_Control__c(id = objAUpdated.Id);
            j.Status__c = 'Completed';
            lstJobControl.add(j);
            }

            if(lstJobControl.size() > 0){
            update lstJobControl;
            } */
        
    }
    
    
    private String getQuery(String sObjectName, String sUniqueId)
    {
        List<trigger_mapping__mdt> lstFieldMapping = [SELECT Source_Field__r.DeveloperName, Source_Object__c, Target_Field__r.DeveloperName, Target_Object__r.DeveloperName FROM trigger_mapping__mdt WHERE Target_Object__r.DeveloperName =:sObjectName ];
        String sQuery = 'SELECT Id ';
        for(trigger_mapping__mdt oMDT: lstFieldMapping)
        {
            sQuery += ',' + oMDT.Source_Field__r.DeveloperName + '__c';   
        }
        //check for std object condition before constructing query
        sQuery += ' FROM Staging_Course__c' + ' WHERE Unique_Id__c =: sUniqueId';
        return sQuery;
    }
    
    
    private sObject createNewRecordFromMapping(Staging_Course__c oStag, String sObjectName)
    {
        List<trigger_mapping__mdt> lstFieldMapping = [SELECT Source_Field__r.DeveloperName, Source_Object__c, Target_Field__r.DeveloperName, Target_Object__r.DeveloperName FROM trigger_mapping__mdt WHERE Target_Object__r.DeveloperName =:sObjectName ];
        //sObject sObjectNew = new sObject(Type = lstFieldMapping[0].Target_Object__r.DeveloperName +'__c');
        sObject sObjectNew = Schema.getGlobalDescribe().get(lstFieldMapping[0].Target_Object__r.DeveloperName +'__c').newSObject();
        for(trigger_mapping__mdt oMDT: lstFieldMapping)
        {
            sObjectNew.put(oMDT.Target_Field__r.DeveloperName + '__c', oStag.get(oMDT.Source_Field__r.DeveloperName + '__c'));
        }
        
        return sObjectNew;
        
    }
}

The commented part is throwing the error.
Jayant DasJayant Das
Are you not getting error on line 35? You cannot modify a collection while iterating over it. If you want to perform a DML operation on the records received, you will need to create a new list and add the updated records to the new list and then perform the DML operation.
Srikanth sfdc 32Srikanth sfdc 32
HI

try this code.

String sQueryForUniqueId = getQuery(sObjectName, sUniqueId);

List<Job_Control__c> lstJobControl =Database.query(sQueryForUniqueId);

List<Job_Control__c> lstJobControl12 =Database.query(sQueryForUniqueId);


for(Job_Control__c objAUpdated : lstJobControl)
           {
            Job_Control__c j = new Job_Control__c(id = objAUpdated.Id);
            j.Status__c = 'Completed';
            lstJobControl12.add(j);
            }
 
            if(lstJobControl12.size() > 0){
            update lstJobControl12;
            } 
         
    }