0

I have 3 objects. I am migrating data from one object to two objects. For example I have Account1, Account2, Account3 objects. I am migrating Data from Account1 to Account2 and Account3. Here Account2 is master of Account3. First I am migrating Account1 to Account2. I am storing Account1 ID into customfield called "Account1ReferenceID" in Account2 while migrating. Now I am migrating Account1 to Account3. Here in Account3 I need to map the parent name in Account2. By using "Account1ReferenceID" in Account2. Here is my code for Account1 to Account3. I know using maps I can get it but I am not familiar with maps.

List<Account1__c> acc1=new List<Account1__c>();
acc1=[select ID, Name from Account1__c ];

List<Account2__c> acc2=new List<Account2__c>();
acc2= [Select ID, Name, Account1ReferenceId__c from Account2__c where Accoun1ReferenceId__c!=null];

List<Account3__c> acc3=new List<Account3__c>();

for(Account1__c account1: acc1){
   Account3__c account3 = new Account3__c();
        account3.Name = acc1.name;
        account3.Account2 = 
        // Account2 is master. How can i find the master in Account2.
        acc3.add(account3);
}
try{
    insert acc3;
}
catch(Exception e){
    System.debug('The following exception has occurred: ' + e.getMessage());
}
3
  • What do you mean by migrating?Using confusing words may not attract answers to your question. Choose technical jargon wisely.
    – Jarvis
    Jan 26, 2016 at 15:07
  • transferring data from Account1 to Account2 & Account3. Jan 26, 2016 at 15:13
  • I'll be curious, is this not something you can achieve by using account hierarchy and save yourself the migration? Jan 26, 2016 at 15:52

1 Answer 1

0

Try this code. it will help you.

    map<Id,Account1__c > acc1map = new map<Id,Account1__c >([select ID, Name from Account1__c ]);


    map<Id,Id> acc2map = new map<Id,Id>()
 for(Account2__c  acc: [Select ID, Name, Account1ReferenceId__c from Account2__c where Accoun1ReferenceId__c!=null])
       acc2map.put(acc.Accoun1ReferenceId__c, acc.Id);

    List<Account3__c> acc3=new List<Account3__c>();
    for(Account1__c account1: acc1map.values()){
            Account3__c account3 = new Account3__c();
            account3.Name = account1.name;
            if(acc2map.containsKey(account1.Id))
               account3.Account2 = acc2map.get(account1.Id);
            acc3.add(account3);
    }
    try{
        insert acc3;
    }
    catch(Exception e){
        System.debug('The following exception has occurred: ' + e.getMessage());
    }
1
  • 1
    Thanks, Thushar Sharma. I found the solutions my self. My code also follows same way. Jan 27, 2016 at 10:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .