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
FebreezeFebreeze 

Custom setting- Batch apex

Hi, I have a requirement wherein , i have a custom setting with records that have fields as below:
1.Source Object
2.Source Field
3.Destination Object
4.Destination Field
for example if i have a record with such field values
1.Source Object: Account
2.Source Field:Name
3.Destination Object:Contact
4.Destination Field:LastName

then i have to write a batch apex that will create records into destination object with the given destination field with value  , from source objects  source values...it will be reflected as below:

Account- Name:Rahul then after running the batch apex
Contact-LastName : Rahul. 

please help me resolve it 
Thanks in advance.
Himanshu ParasharHimanshu Parashar
Hi  Febreeze,

You can achieve this using the help of SObject, I am assuming that you know how to write batch, how to read value from Custom setting so I am writing code after that.
 
String strSourceObjectname = 'Account'; // Get value from Custom setting
String strSourceFieldname = 'Name' ; //Get value from custom setting
String strTargetObjectname ='Contact';
String strTargetObjectField = 'Lastname';
List<SObject> lstSObject = new List<sObject>();

//Get Data for source object
Sobject sobj=database.query('select id,' + strSourceFieldname + ' from '  + strSourceObjectname + ' limit 1');

//Get data from Account object
String strName = sobj.get(strSourceFieldname);

//Create insert
Schema.SObjectType targetType = Schema.getGlobalDescribe().get(strTargetObjectname);
sObject sObj = targetType.newSObject();
sObj.put(strTargetObjectField,strName);
ls.add(sObj);
insert ls;

I hope that this will help you.


Thanks,
Himanshu
Salesforce Certified Developer | Administrator | Service Cloud Consultant

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
FebreezeFebreeze
HI , thanks for ur reply
I have achieved this already

global  with sharing class BatchCustomSetting implements Database.Batchable<sObject> , Database.Stateful{
    
    global String Query;
    
    global String queryloop;
    
    global List<String> queryList = new List<String>();
    
    global List<List<SObject>> objectList = new List<List<SObject>>();
    
    global map<SObject , String> sourcemapping =new map<SObject , String>();
    
    global map<String , String> sourceMap = new map<String , String>();
    
    global map<String , String> destinationMap = new map<String , String>();
    
    global map<String , String> fieldMap = new  map<String , String>();
    
    global map<String , String> objectMap = new map<String , String>();
    
    global Database.QueryLocator start(Database.BatchableContext BC){
        
        return Database.getQueryLocator([Select Id,SourceField__c,SourceObject__c,DestinationObject__c,DestinationField__c
                                          FROM ObjectMapping__c]);
                                          
    }
        
    global void execute(Database.BatchableContext BC, List<ObjectMapping__c> scope){
        
        for(ObjectMapping__c scopeobj : scope){
            
            sourceMap.put(scopeobj.SourceObject__c , scopeobj.SourceField__c);
            
            fieldMap.put(scopeobj.SourceField__c , scopeobj.DestinationField__c);
            
            destinationMap.put(scopeobj.DestinationObject__c , scopeobj.DestinationField__c );
            
            objectMap.put(scopeobj.SourceObject__c , scopeobj.DestinationObject__c);
            
            Query = 'Select ' +scopeobj.SourceField__c+ ' from ' +scopeobj.SourceObject__c;
            
            queryList.add(Query);
            
            objectList.add(Database.Query(Query));
            
        }
        for(list<SObject> sobjlistsobj : objectList){
            
            String sourcestring = string.ValueOf(sobjlistsobj.getSobjectType());

            Schema.SObjectType destinationType = Schema.getGlobalDescribe().get(objectMap.get(sourcestring));

            String destinationString = destinationMap.get(objectMap.get(sourcestring));

            SObject destinationObj = destinationType.newSObject();
            destinationObj .put(destinationString  , '???');
            
        }
    }
    global void finish(Database.BatchableContext BC){
    
    }


i am stuck  at the insertion part ... not able to proceed from here ...  like how do i get all the values for a particular sobject field ??? do i have to create a set , but if i do the same how do i iterate on that ???
Narasimha Reddy 69Narasimha Reddy 69
Simply you can Use workflow to update the field name, if have relationship between the objects  , instead of writing the code in Apex.