+ Start a Discussion
SFDC DummySFDC Dummy 

Papulate all record in another object when trigger fire

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 have created a trigger but it will be created or upadted only one record which i am creating.if i have 10 record in object1 it will be papulate all 10 record in 
object2 when trigger fire...


 
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;
}

 
Best Answer chosen by SFDC Dummy
Abhishek BansalAbhishek Bansal
Hi,

Please find your required code :

trigger UpsertMasterNew on Debtors_Ledger__c(after insert,after update) {
    List<Debtors_Ledger__c> allDebtorData = new List<Debtors_Ledger__c>([Select Name,DailyUpdate__c,Debtor_Name__c,Credit_formula__c,Debit_formula__c,Group__c from Debtors_Ledger__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]);
    
    List<MasterCopy__c> insertMasterList = new List<MasterCopy__c>();
    MasterCopy__c newMaster;
    
    Date updatedDate = trigger.new[0].DailyUpdate__c;
    if(allMasterData.size() == 0){
        for(Debtors_Ledger__c debtor : allDebtorData){
            newMaster = new MasterCopy__c();
            newMaster.Master_Code__c= debtor.Name;
            newMaster.Date__c = updatedDate;
            newMaster.Master_Name__c= debtor.Debtor_Name__c;
            newMaster.Credit__c= debtor.Credit_formula__c;
            newMaster.Debit__c= debtor.Debit_formula__c;
            newMaster.Group__c= debtor.Group__c;
            insertMasterList.add(master);
        }
    }
    else {
        if(trigger.isUpdate){
            for(Debtors_Ledger__c debtor : trigger.new){
                if(trigger.oldmap.get(debtor.id).DailyUpdate__c != debtor.DailyUpdate__c){
                    for(MasterCopy__c master : allMasterData){
                        master.Date__c = debtor.DailyUpdate__c;
                    }
                }
            }
        }
        if(trigger.isInsert){
            for(Debtors_Ledger__c debtor : trigger.new){
                if(allMasterData[0].Date__c != debtor.DailyUpdate__c){
                    for(MasterCopy__c master : allMasterData){
                        master.Date__c = debtor.DailyUpdate__c;
                    }
                }
            }
        }
    }
    allMasterData.addAll(insertMasterList);
    upsert allMasterData;
}



One more thing, If u still find any issues or any case that is not working than please explain the case which is not working properly and also the case which is working fine so that i can have a close look and will update the code accordingly.

Regards,
Abhishek.

All Answers

Abhishek BansalAbhishek Bansal
You said that there are no records in object 2 initially but after that you have written that when you update record of object1 than all records of object2 should be updated.
Please explain me form where the records have created in Object2 if there were no records initially.

According to your requirement there will be only a single record in object2 which will be updated each and every time when a new object1 record is created or updated.

Please clear out your requirement in a better way.
 
SFDC DummySFDC Dummy
Yes Abhishek . initially object1 have 10 record and object2 have no record..ok when i am updating one record of object1 with date value 2/1/2015 and when i am going to save it will papulate all the 10 record of object1 will be created in object2. means now object2 have 10 record. if i am updating any record of object1 with date value 2/1/2015 .it will only update the record of object2 all record. if i am updating any record of object1 with date value 3/1/2015 (i means date value changed).it will again create another 10 record with changed date value in object2..like this i want
Abhishek BansalAbhishek Bansal
Based on your reply this is what you want to achieve :

1. Initially when there are no records on Object2 and Any record of object1 is changed (only date is changed) than you will create 10 records in object 2 and all 10 records of obejct will have same date.

2. Now both object1 and object2 have same no. of records i.e. 10. If i again change the data value in any record of object 1 than again 10 records will be created in obejct 2 with the changed date. That means that obejct 1 still have 10 records and object 2 have 20 records.

Now please clear following points :
1. What should be done when object2 have 10 records and Any record of obejct1 is updated but date is not changed.
2. What happen in the case of insertion. Please explain all cases like:
  • When there are no records in object2 and a new object1 record is inserted ?
  • When Object2 have 10 records than a new record with same date is inserted ?
  • When Object2 have 10 records than a new record with different date is inserted ?
Please clear above queries.

Regards,
Abhishek.
SFDC DummySFDC Dummy
Abhishek when object2 have 10 records and Any record of obejct1 is updated but date is not changed means it will only update the record. When there are no records in object2 and a new object1 record is inserted .then it will papulate all record in object2 When Object2 have 10 records than a new record with same date is inserted--it will be inserted in object 2 like this
Abhishek BansalAbhishek Bansal
Please send complete information.
Abhishek BansalAbhishek Bansal
Hi,

Please find your required code :

trigger UpsertMasterNew on Debtors_Ledger__c(after insert,after update) {
    List<Debtors_Ledger__c> allDebtorData = new List<Debtors_Ledger__c>([Select Name,DailyUpdate__c,Debtor_Name__c,Credit_formula__c,Debit_formula__c,Group__c from Debtors_Ledger__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]);
    
    List<MasterCopy__c> insertMasterList = new List<MasterCopy__c>();
    MasterCopy__c newMaster;
    
    Date updatedDate = trigger.new[0].DailyUpdate__c;
    if(allMasterData.size() == 0){
        for(Debtors_Ledger__c debtor : allDebtorData){
            newMaster = new MasterCopy__c();
            newMaster.Master_Code__c= debtor.Name;
            newMaster.Date__c = updatedDate;
            newMaster.Master_Name__c= debtor.Debtor_Name__c;
            newMaster.Credit__c= debtor.Credit_formula__c;
            newMaster.Debit__c= debtor.Debit_formula__c;
            newMaster.Group__c= debtor.Group__c;
            insertMasterList.add(master);
        }
    }
    else {
        if(trigger.isUpdate){
            for(Debtors_Ledger__c debtor : trigger.new){
                if(trigger.oldmap.get(debtor.id).DailyUpdate__c != debtor.DailyUpdate__c){
                    for(MasterCopy__c master : allMasterData){
                        master.Date__c = debtor.DailyUpdate__c;
                    }
                }
            }
        }
        if(trigger.isInsert){
            for(Debtors_Ledger__c debtor : trigger.new){
                if(allMasterData[0].Date__c != debtor.DailyUpdate__c){
                    for(MasterCopy__c master : allMasterData){
                        master.Date__c = debtor.DailyUpdate__c;
                    }
                }
            }
        }
    }
    allMasterData.addAll(insertMasterList);
    upsert allMasterData;
}



One more thing, If u still find any issues or any case that is not working than please explain the case which is not working properly and also the case which is working fine so that i can have a close look and will update the code accordingly.

Regards,
Abhishek.
This was selected as the best answer
Abhishek BansalAbhishek Bansal
Is your requiremnet achieved or not ?