You need to sign in to do that
Don't have an account?
Febreeze
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.
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.
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.
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.
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 ???