You need to sign in to do that
Don't have an account?
sk3501.3915272120779841E12
Record transfer from one object to another in same org of salesforce
Hi, i want to transfer record from one object to another object in same org. Below is my coding i want to do this using batch apex, i am almost successful exception name no other record is deploying, please help and rectify whether i am wrong.
global class RecordTransfer implements database.batchable<sObject>{
global final String Query;
global RecordTransfer(String q){
Query = q;
}
global Database.queryLocator start(Database.BatchableContext BC){
//Query = 'Select id, Book_Title__c from Book__c';
return Database.getQueryLocator(Query);
}
global void execute(Database.BatchableContext BC, List<sObject> scope){
Set<String> setFields = new Set<String>{'name','Book_Title__c','Description__c'};
Map<String, Schema.sObjectField> schemaFieldMap = Schema.sObjectType.Book__c.fields.getMap();
List<BookTwo__c> accList = new List<BookTwo__c>();
for(sObject s : scope){
BookTwo__c tempBook = new BookTwo__c();
for(String fieldName:schemaFieldMap.keySet()){
if(setFields.contains(fieldName)){
tempBook.put(fieldName,s.get(fieldName));
System.debug('????? tempBook '+tempBook);
}
}
accList.add(tempBook);
system.debug('????? accList '+accList);
}
insert accList;
}
global void finish(Database.BatchableContext BC){
AsyncApexJob a = [Select Id, Status, NumberOfErrors, JobItemsProcessed, TotalJobItems, CreatedBy.Email From AsyncApexJob Where Id=:BC.getJobId()];
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {a.Createdby.Email};
mail.setToAddresses(toAddresses);
mail.setSubject('Record Transefer Status: '+a.status);
mail.setPlainTextBody('The Batch Apex job processed '+a.TotalJobItems +
' batches with '+a.NumberOfErrors + ' failures. ');
Messaging.sendEmail(New Messaging.SingleEmailMessage[] {mail});
}
}
global class RecordTransfer implements database.batchable<sObject>{
global final String Query;
global RecordTransfer(String q){
Query = q;
}
global Database.queryLocator start(Database.BatchableContext BC){
//Query = 'Select id, Book_Title__c from Book__c';
return Database.getQueryLocator(Query);
}
global void execute(Database.BatchableContext BC, List<sObject> scope){
Set<String> setFields = new Set<String>{'name','Book_Title__c','Description__c'};
Map<String, Schema.sObjectField> schemaFieldMap = Schema.sObjectType.Book__c.fields.getMap();
List<BookTwo__c> accList = new List<BookTwo__c>();
for(sObject s : scope){
BookTwo__c tempBook = new BookTwo__c();
for(String fieldName:schemaFieldMap.keySet()){
if(setFields.contains(fieldName)){
tempBook.put(fieldName,s.get(fieldName));
System.debug('????? tempBook '+tempBook);
}
}
accList.add(tempBook);
system.debug('????? accList '+accList);
}
insert accList;
}
global void finish(Database.BatchableContext BC){
AsyncApexJob a = [Select Id, Status, NumberOfErrors, JobItemsProcessed, TotalJobItems, CreatedBy.Email From AsyncApexJob Where Id=:BC.getJobId()];
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {a.Createdby.Email};
mail.setToAddresses(toAddresses);
mail.setSubject('Record Transefer Status: '+a.status);
mail.setPlainTextBody('The Batch Apex job processed '+a.TotalJobItems +
' batches with '+a.NumberOfErrors + ' failures. ');
Messaging.sendEmail(New Messaging.SingleEmailMessage[] {mail});
}
}
Try this and see if it works! if not, please share the debug log so that it will be easy to identify!!
Thanks,
balaji
All Answers
Map Keys in salesforce were case sensitive, so change your setFeild declaration statement in the below way:
Set<String> setFields = new Set<String>{'Name','Book_Title__c','Description__c'};
Hope it works:
Thanks,
Balaji
It is depoying the name but rest of the fields record is not coming.
Try this and see if it works! if not, please share the debug log so that it will be easy to identify!!
Thanks,
balaji
i have one more question regarding this let's suppose the first object Book__c contains the field name somenamespace__fieldname now how will resolve this with
if(setFields.contains(fieldName.toLowerCase()))
i will have to trim the namespace first then check the setFields. so how would i do that.
This is working for all fields but the it is not working for Description field whose data type is long text area. I don't know why it is not showing record of Description field.
Hi,
The problem has been solved. Thanks once again for your reply.