+ Start a Discussion
SFDC DummySFDC Dummy 

How to write trigger to copy all existing record to another object:urgent

Hi Friends

I have facing a challenge long time but i am not getting the solution.

I have two custom object.(object1 and object2).
object1 have some reocrd where in object2 there no record initially.when i am creating record or updating record it will be papulate all record of object1 to object2 .

like if object1 have recrod

1---100-----1/2/2010
2---200-----2/2/2010
3---300-----3/2/2010........when i am updating record on 1---100-----1/2/2010 to 1---500-----10/2/2010 on object1

it will be papulate in object2  like
1---500-----10/2/2010
2---200-----10/2/2010
3---300-----10/2/2010........means it will papulate all existing reccord with updated date field.if i am upadting again with same date it
will be updated on same record.if i am creating record with diffrent date means date not same as before it will be again papulate all existing field with updated date field..

like  when i am updating record on 1---100-----10/2/2010 to 1---1000-----15/2/2010 on object1

it will be papulate in object2  like
1---1000-----15/2/2010
2---200-----15/2/2010
3---300-----15/2/2010......

i ahve created a trigger but it will be created or upadted only one record which i am creating
 
trigger UpsertMasterNew on Debtors_Ledger__c(after insert,after update) {
    
    Set<Date> dateSet = new Set<Date>();
    for(Debtors_Ledger__c debtor : trigger.new)
    {
        dateSet.add(debtor.DailyUpdate__c);
    }
    
    List<MasterCopy__c> allMasterData = new List<MasterCopy__c>([Select Name,Date__c,Group__c,Master_Code__c,Master_Name__c,Credit__c,Debit__c from MasterCopy__c where Date__c IN : dateSet]);
    
    Map<Date,MasterCopy__c> masterDataMap = new Map<Date,MasterCopy__c>();
    
    for(MasterCopy__c masData : allMasterData){
        masterDataMap.put(masData.Date__c,masData);
    }
    List<MasterCopy__c> insertMasterList = new List<MasterCopy__c>();
    MasterCopy__c master;
    for(Debtors_Ledger__c debtor : trigger.new){
        if(masterDataMap.containsKey(debtor.DailyUpdate__c)){
            masterDataMap.get(debtor.DailyUpdate__c).Master_Code__c= debtor.Name;   // masterDataMap.get(debtor.DailyUpdate__c).Name + ' ' +
           masterDataMap.get(debtor.DailyUpdate__c).Credit__c=  debtor.Credit_formula__c;
           masterDataMap.get(debtor.DailyUpdate__c).Debit__c=  debtor.Debit_formula__c;
           masterDataMap.get(debtor.DailyUpdate__c).Group__c=  debtor.Group__c;
           masterDataMap.get(debtor.DailyUpdate__c).Master_Name__c=  debtor.Debtor_Name__c;
           masterDataMap.get(debtor.DailyUpdate__c).Date__c =  debtor.DailyUpdate__c;
        }
        else{
            master = new MasterCopy__c();
            master.Master_Code__c= debtor.Name;
            master.Date__c = debtor.DailyUpdate__c;
            
            master.Master_Name__c= debtor.Debtor_Name__c;
            master.Credit__c= debtor.Credit_formula__c;
            master.Debit__c= debtor.Debit_formula__c;
            master.Group__c= debtor.Group__c;
            insertMasterList.add(master);
        }
    }
    insertMasterList.addAll(masterDataMap.values());
    upsert insertMasterList;
}
it will be papulate like thiss....
User-added image
 
Best Answer chosen by SFDC Dummy
SFDC DummySFDC Dummy
Can u share the modified trigger.what you are saying

All Answers

David ZhuDavid Zhu

It seems you use Date__c as key in masterDataMap. In Mastercopy__c, there are multiple records with the same Date__c.

In Line 18, you loop through Debtors_Ledger__c to get the date, then use containKey method to get the mastcopy__c in masterDataMap. This will only get the first matching mastercopy__c record. That could cause only one record is updated.

Specifically, masterDataMap.get(debtor.DailyUpdate__c) only returns ONE record.

You may need to loop through masterDataMap inside Debtors_Ledger__c loop (line 18) to update the information.

In addition to that, you can also use system.debug(masterDataMap.values()); to check the actually value for each data record.
SFDC DummySFDC Dummy
Can u share the modified trigger.what you are saying
This was selected as the best answer
David ZhuDavid Zhu
..... 
for(Debtors_Ledger__c debtor : trigger.new)
{
     for (int i = 0;i<masterDataMap.size();i++)
     {
               if(masterDataMap[i].Date__c == debtor.DailyUpdate__c)
               {
                    masterDataMap.get(debtor.DailyUpdate__c).Master_Code__c= debtor.Name;   // masterDataMap.get(debtor.DailyUpdate__c).Name + ' ' +
                    masterDataMap.get(debtor.DailyUpdate__c).Credit__c=  debtor.Credit_formula__c;
                    masterDataMap.get(debtor.DailyUpdate__c).Debit__c=  debtor.Debit_formula__c;
                    masterDataMap.get(debtor.DailyUpdate__c).Group__c=  debtor.Group__c;
                    masterDataMap.get(debtor.DailyUpdate__c).Master_Name__c=  debtor.Debtor_Name__c;
                    masterDataMap.get(debtor.DailyUpdate__c).Date__c =  debtor.DailyUpdate__c;
                }
      }
      
      if (!masterDatMap.containsKey(debtor.DailyUpdate__c))
       {
            master = new MasterCopy__c();
            master.Master_Code__c= debtor.Name;
            master.Date__c = debtor.DailyUpdate__c;
            
            master.Master_Name__c= debtor.Debtor_Name__c;
            master.Credit__c= debtor.Credit_formula__c;
            master.Debit__c= debtor.Debit_formula__c;
            master.Group__c= debtor.Group__c;
            insertMasterList.add(master);
        }
}
 ............

//system.debug(masterDataMap.values());   -- check if the value is changed as expected.
insertMasterList.addAll(masterDataMap.values());
This was not tested. May need to correct any typos.