+ Start a Discussion
sk3501.3915272120779841E12sk3501.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});
    }
}
Best Answer chosen by sk3501.3915272120779841E12
Balaji Chowdary GarapatiBalaji Chowdary Garapati
Then this might help:
 
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.toLowerCase())){
                        system.debug('Field Name ***'+fieldName+'Sobject Value ***'+s.get(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

Balaji Chowdary GarapatiBalaji Chowdary Garapati
@sk3501.3915272120779841E12:

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
 
sk3501.3915272120779841E12sk3501.3915272120779841E12
Hi,
It is depoying the name but rest of the fields record is not coming.
Balaji Chowdary GarapatiBalaji Chowdary Garapati
Then this might help:
 
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.toLowerCase())){
                        system.debug('Field Name ***'+fieldName+'Sobject Value ***'+s.get(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
 
This was selected as the best answer
sk3501.3915272120779841E12sk3501.3915272120779841E12
hi,
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.
Balaji Chowdary GarapatiBalaji Chowdary Garapati
In that case try this little extra added piece
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()){
					//This is written assuming that field api name has __ only in case of "__c" or a packaged/namespace field
					if(fieldName.contains('__') && fieldName.indexOf('__')!=fieldName.IndexOf('__c')){// Verify it has __ other than __c
					    fieldName=fieldName.substring(fieldName.indexOf('__')+2,fieldName.length()); // If so trim it
					}
                    
                    if(setFields.contains(fieldName.toLowerCase())){
                        system.debug('Field Name ***'+fieldName+'Sobject Value ***'+s.get(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});
    }
}
sk3501.3915272120779841E12sk3501.3915272120779841E12
Thanks for your reply!!!

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.
sk3501.3915272120779841E12sk3501.3915272120779841E12

Hi,

The problem has been solved. Thanks once again for your reply.