I have a batch apex which takes the record from source object (can vary like Opportunity, Opportunity line item etc) and paste the record into target object i.e Opportunity_Prod_Batch_Record__c (fixed) based on the mapping defined in the floor plan mapping object. Means the source object and its field will be decided by the mapping which is stored in the Floor plan mapping object as shown in the below screen shot.

enter image description here

enter image description here

So the source object can be vary it could be custom object as well. No i have written the batch class to achieve the above result but need help in iterating loop over sobject in execute method as object and its field can vary so i don't know how can i iterate loop over sobject. I have correctly formed the query based on the mapping object screen but unable to update the target object with records. Below is my batch apex.

Global class OppProdCustombatchupdate implements Database.batchable<sobject>,Database.stateful
    string Edition;
    string objects;
    string Opportunity;
    string stage;
    string query;
    global List<Floor_Plan_Mapping__c> LstFlrmapping=new list< Floor_Plan_Mapping__c>();
    Global Database.querylocator Start (Database.batchablecontext bc)

        LstFlrmapping=[select Id, Edition__c,Object__c,Opportunity__c,Stage__c from Floor_Plan_Mapping__c where Id='a4q7E0000013P8E'];

        query= 'select '+LstFlrmapping[0].Edition__c+','+LstFlrmapping[0].Opportunity__c+ ' from '+LstFlrmapping[0].Object__c+' limit 2';


        system.debug('Query formed----> '+query); 

        return database.getquerylocator(query);
    Global Void Execute(Database.batchableContext BC,List<sobject> sc)
        List<Opportunity_Prod_Batch_Record__c> opprec = new List<Opportunity_Prod_Batch_Record__c>();
        for(Sobject sobjet : sc)

            System.debug('Sobject value---->'+sobjet);

            Opportunity_Prod_Batch_Record__c opp = new Opportunity_Prod_Batch_Record__c();

            opp.name = 'test';//'Test Avesh';//Opportunity;
            //opp.Opportunity_Id__c = ol.OpportunityId;
            //opp.Opportunity_Stage__c = s.Opportunity__c;
            opp.Edition_Name__c = sobjet;//'Test Avesh'; //Edition;
            //opp.Stand_No__c = ol.opportunity.Stand_Number_s__c;
            //opp.Banner_name__c = ol.opportunity.Banner__c;
        insert Opprec;

    Global void Finish(Database.BatchableContext bContext){}

Can anyone please guide me how can I modify the above code to get the results.


You'd use the "get" method to get the field value, like this:

opp.Opportunity_Id__c = (Id)sobjet.get(opportunity);

Note: opportunity refers not to Schema.Opportunity in this context, but simply the String variable called opportunity that was set in the start method.

  • System.debug('Sobject value---->'+sobjet); O/P:- Sobject value---->OpportunityLineItem:{Edition_Name__c=ADIPEC 2018, Opportunity_Name__c=utestu / ADIPEC 2018 / 099500, Id=00k7E000007WZhPQAW} System.StringException: Invalid id: utestu / ADIPEC 2018 / 099500 Can u please let me know what is Id?
    – Avesh
    Dec 31 '18 at 6:32
  • Also field can be dynamic sometimes it might be Edition_Name__C or it might be Event_C it may vary based on the org but order(sequence) will be fixed i.e first Edition_name__C and then Opportunity_Name__C and so on
    – Avesh
    Dec 31 '18 at 6:39
  • @Avesh It was hard enough to guess your data types without having weird field names. If that's the case, you're going to need to do more queries somehow. I ffeel there might be a fundamental problem in your code. The "get" method gets a dynamic field from an object.
    – sfdcfox
    Dec 31 '18 at 7:02

Thank you @sfdcfox for your help.

Here is the below-working code

    global class OppProdCustombatchupdate implements Database.Batchable<sObject>,Database.Stateful

String Edition;
String objects;
String Opportunity;
String stage;
String query;
List<Floor_Plan_Mapping__c> LstFlrmapping=new list< Floor_Plan_Mapping__c>();

global Database.QueryLocator Start(Database.BatchableContext bc)

LstFlrmapping=[select Id, Edition__c,Object__c,Opportunity__c,Stage__c from Floor_Plan_Mapping__c where Id='a4q7E0000013P8EQAU'];
query= 'select ID,'+LstFlrmapping[0].Edition__c+','+LstFlrmapping[0].Opportunity__c+ ' from '+LstFlrmapping[0].Object__c+' limit 5';
System.debug('Query formed----> '+query); 

List<sObject> sc= Database.query(query);
return Database.getquerylocator(query);


global void execute(Database.BatchableContext BC,List<sObject> sc)

 Schema.SObjectType sObjectType = sc.getSObjectType();
    if (sObjectType != null)
        String listType = 'List<' + sObjectType + '>';
        List<SObject> castRecords = (List<SObject>)Type.forName(listType).newInstance();
        List<Opportunity_Prod_Batch_Record__c> opprec = new List<Opportunity_Prod_Batch_Record__c>();

        for(INTEGER i=0;i<castRecords.size();i++)
            Opportunity_Prod_Batch_Record__c opp= new Opportunity_Prod_Batch_Record__c();
    opp.Edition_Name__c= (String)castRecords[i].get(Edition);



        upsert opprec;

global void finish(Database.BatchableContext bContext){


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.