[SalesForce] Dynamic Batch apex with sobject iteration

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';

        Edition=LstFlrmapping[0].Edition__c;
        Opportunity=LstFlrmapping[0].Opportunity__c;

        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;
            Opp.Opportunity_Name__c=sobjet;
            opprec.add(opp);
        }
        insert Opprec;

    }
    Global void Finish(Database.BatchableContext bContext){}
}

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

Best Answer

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.

Related Topic